How to Handle Response in Python Requests: Simple Fixes and Tips
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.
import requests response = requests.get('https://example.com') # Incorrect: assuming response is JSON without checking print(response.json())
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.
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}')
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
timeoutparameter 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 ofresponse.content.
Key Takeaways
response.status_code before using the data.response.text for text and response.json() only if the content type is JSON.response.headers['Content-Type'] to confirm the data format.response.content is a property, not a method.