Phase 1 — Foundations · Lesson 6 · 20 XP
HTTP fundamentals: requests, status codes, JSON, httpx
HTTP has four verbs you'll use constantly: GET (read), POST (create), PUT (replace), DELETE (remove). Status codes come in families — 2xx succeeded, 3xx redirected, 4xx you made a bad request, 5xx the server broke. Knowing the family tells you where to look before you even read the message.
import httpx
r = httpx.get("https://api.example.com/users", params={"limit": 5})
r.raise_for_status() # raises on 4xx/5xx instead of silently continuing
data = r.json()httpx is the modern HTTP client for Python — the same API works for both sync (httpx.get) and async calls, which matters once you get to Lesson 7. raise_for_status() turns a bad response into an exception immediately, instead of your code continuing on with garbage data and failing somewhere confusing three lines later.
This is exactly the mechanism you'll use to call an LLM provider's API in Phase 2 — headers carry the API key, the body is JSON, and the response is JSON you parse and validate.
Exercise
Write a function that GETs a REST API, handles a 404 differently from a 500 (one means "doesn't exist", the other means "retry later"), and parses the JSON response into a dataclass.
Check yourself
1. What does raise_for_status() do, and why call it instead of checking response.status_code by hand every time?
2. What's the difference between a 4xx and a 5xx status code, and whose fault is each one usually?
Standard-library CLI: argparse, json, csv (todo app capstone)
Answer the check-yourself questions to unlock this