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 = ''
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:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you

Try ProxiesAPI for free

curl ""

<!doctype html>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />


Don't leave just yet!

Enter your email below to claim your free API key: