Python requests JSON API examples (GET, POST, errors)
Last updated 2026-05-20
When to use this
Use this when you are scripting from Python — data pipelines, scrapers, CLI tools, automation. requests is the de facto HTTP client; httpx is a drop-in for sync + async needs.
Example JSON
Reference JSON payload returned by /users/1 (used in every snippet below).
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
return r.json()
except requests.HTTPError as err:
print('HTTP', err.response.status_code, err.response.json())
raise
except requests.RequestException as err:
print('network error:', err)
raise
Try the live endpoint
Click below to call /users/1 from your browser.
// click the button to populate this block
Common variations
Async with httpx
{
"snippet": "import httpx\n\nasync with httpx.AsyncClient() as client:\n r = await client.get(\"https://jsonexamples.com/users/1\")\n print(r.json())"
}