Advanced·12 min·web · http · requests
HTTP fundamentals with requests
Every web app is just programs talking over HTTP. requests is the standard Python client — clean API, sensible defaults.
The verbs you'll actually use
- GET — read. Query string via
params=. - POST — create / submit. Body via
json=(auto-sets Content-Type) ordata=(form-encoded). - PUT / PATCH — update.
- DELETE — remove.
Reading the response
r.status_code— 200 OK, 404 not found, 500 server error, 401 unauth, 429 rate-limited.r.json()— parse JSON body (raises if not JSON).r.text— raw string body.r.headers— response headers dict.
Things that bite
- Always set a timeout:
requests.get(url, timeout=10). Otherwise a hung server hangs your program. - Use
r.raise_for_status()to turn 4xx/5xx into exceptions instead of silently succeeding. - Session pooling:
s = requests.Session()— reuses TCP connections, huge speedup for many calls to the same host.
Try it
- Call
https://httpbin.org/status/500and observe the status_code. - Set a header
{"X-PyRun": "hello"}and echo it back from/headers.
Practice
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
Progress0 / 3
- Exercise 1
Make a GET request to
https://httpbin.org/getusingrequestsand printr.status_code. In Pyodide, you must callpyodide_http.patch_all()before using requests — the starter does this for you. - Exercise 2
Send a GET to
https://httpbin.org/getwith query params{"q": "python"}. Parse the JSON response and printr.json()["args"]["q"]— httpbin echoes the query string back underargs. - Exercise 3
POST to
https://httpbin.org/postwithjson={"hello": "world"}. httpbin echoes the parsed JSON body back under thejsonkey. Printr.json()["json"]— it should be the dict{"hello": "world"}.