shaare.it

Unexpected Content-Type

7 Dec 2025

1 min read

Unexpected Content-Type

$ python -c "import requests; r=requests.get('https://example.com/json'); print(r.headers.get('Content-Type'))"
text/html; charset=utf-8

Why this happens

Wrong endpoint or negotiation; server returns HTML instead of JSON.

Fix

  • Use correct URL or set Accept: application/json header.

Wrong code

import requests
r = requests.get("https://example.com/json")
r.json()  # raises JSONDecodeError

Fixed code

import requests
r = requests.get("https://api.example.com/data", headers={"Accept": "application/json"})
print(r.headers.get("Content-Type"))
print(r.json())