JSON in HTTP APIs
The requests library parses JSON responses automatically and sends JSON bodies with json=.
The requests library parses JSON responses automatically and sends JSON bodies with json=.
import requests
# GET — auto-parses JSON
resp = requests.get("https://api.example.com/users")
resp.raise_for_status()
users = resp.json() # list of dicts
# POST JSON body
new_user = {"name": "Alice", "email": "alice@example.com"}
r = requests.post("https://api.example.com/users",
json=new_user,
headers={"Authorization": "Bearer " + token})
created = r.json()
Use json= parameter instead of data= to send JSON — it sets Content-Type automatically.