1. The response is empty or HTML
requests' response.json() raises this when the body is empty or is an HTML error page, from a 404, a 500, a 502 from a proxy or a rate limit page. Check the status and the content type before decoding.
import requests
response = requests.get("https://api.example.com/orders", timeout=10)
response.raise_for_status()
if "application/json" not in response.headers.get("Content-Type", ""):
raise ValueError(f"Expected JSON, got: {response.text[:120]}")
data = response.json()