Troubleshooting Python Requests Returning HTML Instead of JSON

Feb 3, 2024 ยท 2 min read

When working with APIs in Python, you may encounter an issue where requesting a JSON endpoint with the Requests library returns HTML instead of the expected JSON data. There are a few common reasons why this can happen:

The API Requires Authentication

Many APIs require authentication via API keys, OAuth tokens, etc. If the authentication headers are missing from the request, the API may return an HTML-formatted 401 Unauthorized error rather than the JSON data.

Always check the API documentation to see if authentication parameters are required. Pass these credentials in the request headers.

import requests

url = 'https://api.example.com/data'
headers = {'Authorization': 'Bearer xxx'} 

response = requests.get(url, headers=headers)

The Endpoint Returns Multiple Response Formats

Some APIs can return both HTML and JSON responses depending on the Accept header sent in the request. The server checks this header to determine which format to respond with.

Explicitly set the Accept header to application/json to ensure you get JSON back:

headers = {'Accept': 'application/json'}

An API Endpoint No Longer Exists

If an API removes or renames an endpoint that your code is calling, the server may return a generic HTML 404 Not Found error page rather than a JSON error.

Double check the endpoint path referenced in the code matches current API documentation. Monitor for 404 errors indicating invalid endpoints.

Carefully handling authentication, Accept headers, and monitoring for HTML responses can help uncover issues with API requests returning HTML instead of JSON in Python. Pay close attention to server responses during testing.

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!