HTTP
HTTP is a stateless request/response protocol: the client sends a request message, the server sends exactly one response. Everything else — sessions, caching, auth — is built by adding identity and metadata to each exchange.
The 60-second answer
Section titled “The 60-second answer”HTTP is an application-layer protocol where each request carries a method, a target, headers and an optional body, and each response carries a status code, headers and an optional body. It is stateless, so state lives in cookies, tokens or server-side stores. HTTP/1.1 sends one request at a time per connection; HTTP/2 multiplexes streams over one TCP connection with header compression; HTTP/3 runs the same semantics over QUIC on UDP, so a lost packet blocks one stream.
Message anatomy
Section titled “Message anatomy”POST /api/orders HTTP/1.1Host: example.comContent-Type: application/jsonAuthorization: Bearer eyJhbGciOi...
{"sku":"ABC-1","quantity":2}
HTTP/1.1 201 CreatedLocation: /api/orders/8891Content-Type: application/json
{"id":8891,"status":"pending"}One request in, one response out, one status code — so “partial success” in a batch endpoint must live in the body.
Methods
Section titled “Methods”| Method | Safe | Idempotent | Body | Use |
|---|---|---|---|---|
GET |
yes | yes | no | Read |
POST |
no | no | yes | Create or trigger |
PUT |
no | yes | yes | Full replace at a known URL |
PATCH |
no | no* | yes | Partial update |
DELETE |
no | yes | optional | Remove |
Safe means no intended state change. Idempotent means repeating produces the same server state,
which is what makes a retry after a timeout legal. HEAD is GET without a body.
PATCH is not idempotent by default; the media type decides. {"op":"increment"} is not,
{"name":"Ana"} is. PUT is a full representation: fields omitted from the body are removed, which
is why partial bodies sent to PUT silently delete data.
Status codes that matter
Section titled “Status codes that matter”| Code | Meaning | Trap |
|---|---|---|
201 |
Created | Should include Location |
202 |
Accepted | Async work queued, result not ready |
204 |
No Content | Must have no body |
301 |
Moved Permanently | Cached forever; may turn POST into GET |
308 |
Permanent Redirect | Preserves method and body |
302/307 |
Temporary redirect | 307 preserves the method, 302 historically does not |
304 |
Not Modified | Bodyless, used with conditional requests |
401 |
Unauthorized | Not authenticated; send WWW-Authenticate |
403 |
Forbidden | Authenticated but not allowed |
422 |
Unprocessable | Well-formed, semantically invalid |
429 |
Too Many Requests | Include Retry-After |
502/504 |
Bad gateway / timeout | Upstream failed or was slow |
The 401 vs 403 distinction is asked constantly. 401 means identity is missing or invalid;
403 means identity is known and denied. Returning 403 for an unauthenticated request tells an
attacker the resource exists. Use 400 for malformed syntax, 409 for a version or duplicate-key
conflict, 503 when you are overloaded.
Headers worth knowing cold
Section titled “Headers worth knowing cold”Host (which site; mandatory in 1.1, enables virtual hosting), Content-Type/Content-Length,
Transfer-Encoding: chunked for an unknown body length, Accept/Accept-Encoding, Authorization
and Cookie, ETag/If-None-Match and Last-Modified/If-Modified-Since for conditional
requests, Cache-Control and Vary, Location. Never send both Content-Length and
Transfer-Encoding: chunked; request smuggling exploits the ambiguity when a proxy and an origin
disagree about which wins.
Content negotiation and keep-alive
Section titled “Content negotiation and keep-alive”Accept: application/json;q=0.9, text/html;q=0.8 — q is a quality weight from 0 to 1. If the
response varies by Accept, Accept-Encoding or Accept-Language, the server must send Vary
listing them, or a shared cache serves the wrong representation to the next client.
HTTP/1.1 keeps connections open by default (opt-in in 1.0, opt-out in 1.1), saving a TCP and TLS handshake per request. Costs: each idle connection holds a file descriptor, servers close idles after a timeout (so pools need retry on idempotent requests), and pools are per-origin — a slow upstream that exhausts the pool stalls unrelated requests to it.
HTTP/1.1 vs HTTP/2 vs HTTP/3
Section titled “HTTP/1.1 vs HTTP/2 vs HTTP/3”| HTTP/1.1 | HTTP/2 | HTTP/3 | |
|---|---|---|---|
| Concurrency | one request per connection | many streams per connection | many streams over QUIC |
| Transport | TCP | TCP | QUIC over UDP |
| HOL blocking | per connection | at the TCP layer | none, streams are independent |
HTTP/2 solved application-level head-of-line blocking but not TCP-level: one lost packet stalls every stream, because TCP delivers bytes in order. HTTP/3 moves to QUIC, where streams are independent and the handshake is combined — one round trip for a new connection, zero on resumption. Server push was removed from browsers; do not present it as a live feature.
TLS handshake basics
Section titled “TLS handshake basics”TLS gives confidentiality, integrity and server authentication.
- TLS 1.2: two round trips before application data. TLS 1.3: one, with the key exchange in the hello messages. Resumption supports 0-RTT early data, which is replayable — never use it for non-idempotent requests.
- The server proves identity with a CA-signed certificate; the client checks the chain, hostname and validity dates.
- Key exchange is usually ephemeral Diffie-Hellman (ECDHE), giving forward secrecy: stealing the long-term key later does not decrypt past sessions.
ALPNnegotiatesh2orhttp/1.1inside the handshake, so HTTP/2 costs no extra round trip.
Rack: HTTP in Ruby terms
Section titled “Rack: HTTP in Ruby terms”Rack is the clearest concrete model of “one request in, one response out”: an object responding to
call(env) that returns [status, headers, body]. env is a plain hash (REQUEST_METHOD,
PATH_INFO, headers as HTTP_ACCEPT) and the body responds to each. Middleware wraps an app,
mutating env on the way in and the response on the way out. See also
Cookies, CSRF and CORS.
Follow-up questions
Section titled “Follow-up questions”- Why does
304have no body, and what does the client do with its cached copy? - What happens to a
POSTon a302versus a307, and why does it matter for payments? - How does HTTP/2 multiplexing interact with one slow database query on the server?
- Where would you terminate TLS, and what breaks if you do it at the load balancer?
- What is request smuggling, and which header ambiguity enables it?
Common traps
Section titled “Common traps”- “401 means forbidden.” 401 is unauthenticated, 403 is unauthorised. Mixing them leaks information and confuses clients about re-authenticating.
- “HTTP/2 fixes head-of-line blocking.” Only at the application layer; TCP loss still stalls all streams.
- “PUT and PATCH are interchangeable.”
PUTreplaces the whole representation; partial updates null omitted fields. - “GET can have a body.” Syntactically allowed, routinely dropped by proxies and caches.