0
0
PythonDebug / FixBeginner · 4 min read

How to Handle Response in Python Requests: Simple Fixes and Tips

Use response = requests.get(url) to get the response, then access content with response.text for text or response.json() for JSON data. Always check response.status_code to ensure the request succeeded before processing the response.
🔍

Why This Happens

Many beginners try to use the requests library but forget to check the response status or incorrectly access the response content, leading to errors or unexpected results.

For example, trying to use response.json() on a non-JSON response causes an error.

python
import requests

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

# Incorrect: assuming response is JSON without checking
print(response.json())
Output
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
🔧

The Fix

First, check if the request was successful by verifying response.status_code. Then, access the response content properly: use response.text for plain text or response.json() only if the response is JSON.

Use response.headers.get('Content-Type', '') to confirm the content type before parsing JSON.

python
import requests

response = requests.get('https://jsonplaceholder.typicode.com/todos/1')

if response.status_code == 200:
    if 'application/json' in response.headers.get('Content-Type', ''):
        data = response.json()
        print(data)
    else:
        print(response.text)
else:
    print(f'Request failed with status code {response.status_code}')
Output
{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}
🛡️

Prevention

Always check response.status_code before processing the response to avoid errors. Confirm the content type with response.headers before parsing JSON. Use try-except blocks to handle unexpected errors gracefully.

Following these steps helps avoid crashes and makes your code more reliable.

⚠️

Related Errors

Common related errors include:

  • Timeout errors: Use timeout parameter in requests to avoid hanging.
  • Connection errors: Handle with try-except to catch network issues.
  • Attribute errors: Occur if you try to access response content incorrectly, e.g., response.content() instead of response.content.

Key Takeaways

Always check response status with response.status_code before using the data.
Use response.text for text and response.json() only if the content type is JSON.
Check response.headers['Content-Type'] to confirm the data format.
Use try-except blocks to handle errors like JSON decode or connection issues.
Avoid calling methods incorrectly; for example, response.content is a property, not a method.