requests POST in Python
Sending JSON and headers in a POST request.
import requests
response = requests.post(
"https://api.example.com/articles",
json={"title": "on typing", "tags": ["code", "practice"]},
headers={"Authorization": "Bearer token123"},
timeout=10,
)
created = response.json()
new_id = created["id"]
location = response.headers.get("Location")
ok = response.ok
How it works
json=serializes the body and sets the header.- Custom headers carry auth tokens.
- The parsed response yields the new resource.
The run, in numbers
- Lines
- 13
- Characters to type
- 308
- Tokens
- 87
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 50 seconds.
Step 2 of 5 in requests, step 2 of 9 in Web scraping.