Why this matters
Every API you build and every frontend you ship speaks HTTP. Bugs that look mysterious (CORS errors, stale pages, a 502 from a proxy, a streaming response that arrives all at once) are HTTP behaviour you can reason about once you know the layers.
What happens when you open a URL
- DNS: the browser asks resolvers for the IP address of
example.com(A/AAAA records; CNAMEs point names at other names). - TCP connection to that IP on port 443 (QUIC over UDP for HTTP/3).
- TLS handshake: the server proves its identity with a certificate; both sides agree keys; everything after is encrypted.
- HTTP request: method, path, headers, optional body.
- The request usually passes a CDN or reverse proxy (Nginx, a load balancer) before reaching your application server.
- HTTP response: status code, headers, body.
- The browser parses HTML, fetches CSS, JavaScript and images (more requests), builds the page and runs scripts.
HTTP essentials
| Method | Meaning | Safe? | Idempotent? |
|---|---|---|---|
| GET | Read | Yes | Yes |
| POST | Create or trigger an action | No | No (make it so with an idempotency key) |
| PUT | Replace | No | Yes |
| PATCH | Partial update | No | Not necessarily |
| DELETE | Remove | No | Yes |
| Status | Meaning | Who should act |
|---|---|---|
| 200 / 201 / 204 | OK / created / no content | — |
| 301 / 302 / 304 | Moved / found elsewhere / not modified (use cache) | Client follows |
| 400 / 422 | Bad request / valid JSON but invalid content | Client fixes input; don't retry |
| 401 / 403 | Not authenticated / authenticated but not allowed | Client logs in / stop |
| 404 / 409 | Not found / conflict (e.g. duplicate) | Client |
| 429 | Too many requests (read Retry-After) |
Client waits, then retries |
| 500 / 502 / 503 / 504 | Server bug / bad upstream / unavailable / upstream timeout | Server; client may retry with backoff |
Headers you'll meet daily: Content-Type, Authorization, Cache-Control, ETag, Set-Cookie, Retry-After, Location, X-Request-ID.
Cookies, CORS and caching
- Cookies are set by the server and sent back automatically on every request to that site. Mark session cookies
HttpOnly(no JavaScript access),Secure(HTTPS only) andSameSite=LaxorStrict. - CORS is the browser asking the API "may a page from origin A call you?". The API answers with
Access-Control-Allow-Originand friends. CORS protects users' browsers; it isn't authentication, andcurlignores it entirely. - Caching:
Cache-Control: max-age=3600lets caches reuse a response;no-storeforbids it (use for anything personal).ETag+If-None-Matchlets clients revalidate cheaply (304).
Streaming responses
Some responses arrive in pieces: file downloads, Server-Sent Events (SSE) for live updates, and token-by-token LLM output. Proxies love to buffer responses, which turns a stream into one late lump. That's why AI streaming endpoints send Cache-Control: no-cache and, behind Nginx, X-Accel-Buffering: no (Q14).
Common mistakes
- Returning 200 with
{"error": ...}in the body. Use the status code; clients and monitoring depend on it. - "Fixing" CORS with
Access-Control-Allow-Origin: *on an API that uses cookies. - Caching personalised responses at a CDN.
- Retrying 4xx errors. They'll fail the same way, and on paid APIs you pay again.
Interview follow-ups
- "What happens when you type a URL and press enter?" Walk the layers: DNS, TCP/QUIC, TLS, HTTP request, CDN/proxy, app, response, parsing, subresource requests, render. Depth on one layer impresses more than breadth on all.
- "HTTP/1.1 vs HTTP/2 vs HTTP/3?" /2 multiplexes many requests over one connection with header compression; /3 runs over QUIC (UDP), avoiding head-of-line blocking at the transport layer.
- "Why does my API work in curl but fail in the browser?" Usually CORS (preflight failing or missing headers) or cookies (SameSite, Secure, domain).
Check yourself
What's the difference between 401 and 403?
401: we don't know who you are (missing or invalid credentials). 403: we know who you are and you're not allowed.
Which status codes are worth retrying, and which never?
Retry 429 (respecting Retry-After), 502, 503, 504 and network errors, with backoff and a limit. Never retry 400, 401, 403, 404 or 422: the same request fails the same way.
Your streaming endpoint works locally but arrives all at once in production. Likely cause?
A proxy or load balancer buffering the response. Disable buffering for that route (X-Accel-Buffering: no for Nginx), set Cache-Control: no-cache, and check any compression layer.
Build it
Use curl -v https://example.com and identify the DNS result, TLS handshake and response headers. Then open your browser's Network tab on any site and find one cached response (304 or "from cache") and one cookie with its flags.
What comes next
1.4 Python for engineers: the backend language of most AI work.