typestar

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

  1. json= serializes the body and sets the header.
  2. Custom headers carry auth tokens.
  3. 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.

Type this snippet

Step 2 of 5 in requests, step 2 of 9 in Web scraping.

← Previous Next →