Unexpected Content-Type
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/jsonheader.
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())