python requests error handling best practices

Python Requests Error Handling Best Practices

If you are working with APIs or web services in Python, chances are you are using the requests library. Requests is a popular library for making HTTP requests in Python. However, your code can break if you don't handle errors properly. In this post, I will share some best practices for error handling with requests library in Python.

1. Check the response status code

When you make a request with requests library, you get a response object. The response object has a status code attribute that tells you the status of the response. A status code of 200 means the request was successful, while a status code of 400 or higher means there was an error.


import requests

response = requests.get('https://example.com')

if response.status_code == 200:
    print('Request successful')
else:
    print(f'Request failed with status code {response.status_code}')

2. Handle exceptions

Requests can raise exceptions for various reasons, such as network errors, timeouts, and invalid URLs. You should catch these exceptions and handle them appropriately.


import requests

try:
    response = requests.get('https://example.com')
except requests.exceptions.RequestException as e:
    print(f'Request failed with exception {e}')

3. Use the raise_for_status() method

The raise_for_status() method is a convenient way to raise an exception if the response status code is not 200.


import requests

response = requests.get('https://example.com')

response.raise_for_status()

print('Request successful')

4. Handle JSON decoding errors

If you are working with JSON data, you should handle JSON decoding errors. JSON decoding errors can occur if the response is not valid JSON.


import requests
import json

response = requests.get('https://example.com')

try:
    data = response.json()
except json.JSONDecodeError as e:
    print(f'Response is not valid JSON: {e}')

5. Use timeouts

If your code is making requests to external APIs or services, you should use timeouts to prevent your code from hanging indefinitely if the service is down or slow to respond.


import requests

try:
    response = requests.get('https://example.com', timeout=5)
except requests.exceptions.Timeout as e:
    print('Request timed out')

These are some best practices for error handling with requests library in Python. By following these practices, you can write more robust code that can handle errors gracefully.