Skip to content

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.

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.

POST /api/orders HTTP/1.1
Host: example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOi...
{"sku":"ABC-1","quantity":2}
HTTP/1.1 201 Created
Location: /api/orders/8891
Content-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.

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.

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.

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.

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 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 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.
  • ALPN negotiates h2 or http/1.1 inside the handshake, so HTTP/2 costs no extra round trip.

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.

  1. Why does 304 have no body, and what does the client do with its cached copy?
  2. What happens to a POST on a 302 versus a 307, and why does it matter for payments?
  3. How does HTTP/2 multiplexing interact with one slow database query on the server?
  4. Where would you terminate TLS, and what breaks if you do it at the load balancer?
  5. What is request smuggling, and which header ambiguity enables it?
  • “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.” PUT replaces the whole representation; partial updates null omitted fields.
  • “GET can have a body.” Syntactically allowed, routinely dropped by proxies and caches.