HTTP in Depth
Request/response anatomy, methods with safety and idempotency, the status codes that matter, important headers, HTTP caching (Cache-Control, ETag, conditional requests), cookies and their security flags, CORS and preflights, content negotiation and compression, and HTTP/1.1 vs HTTP/2 vs HTTP/3.
HTTP in one picture
HTTP is a stateless, request/response, application-layer protocol. A client sends a request; the server returns a response. State (sessions) is layered on top with cookies or tokens.
HTTP
POST /v1/orders HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOi...
Content-Type: application/json
Accept: application/json
Idempotency-Key: 6f1c0b8e-2c1a-4b7e-9d0a-3c4e5f6a7b8c
Content-Length: 58
{"items":[{"sku":"MOUSE-1","qty":2}],"currency":"INR"}
HTTP
HTTP/1.1 201 Created
Location: /v1/orders/ord_981
Content-Type: application/json
ETag: "v1"
Cache-Control: no-store
{"id":"ord_981","status":"pending","total_cents":99800}
Anatomy: request line (method, target, version) → headers → blank line → optional body. Response: status line → headers → body.
Methods
| Method | Purpose | Safe | Idempotent | Body | Cacheable |
|---|---|---|---|---|---|
| GET | read a resource | ✓ | ✓ | no (semantically) | ✓ |
| HEAD | headers only | ✓ | ✓ | no | ✓ |
| OPTIONS | capabilities, CORS preflight | ✓ | ✓ | optional | ✗ |
| POST | create / process / action | ✗ | ✗ | yes | rarely |
| PUT | replace (or create at a known URI) | ✗ | ✓ | yes | ✗ |
| PATCH | partial update | ✗ | ✗ (can be made so) | yes | ✗ |
| DELETE | remove | ✗ | ✓ | optional | ✗ |
Status codes that matter
| Code | Use it when |
|---|---|
| 200 OK | success with a body |
| 201 Created | resource created (Location header) |
| 202 Accepted | work queued; poll a status URL |
| 204 No Content | success, nothing to return (DELETE, PUT) |
| 301 / 308 | permanent redirect (308 keeps method/body) |
| 302 / 307 | temporary redirect (307 keeps method/body) |
| 303 See Other | after a POST, redirect to a GET |
| 304 Not Modified | conditional GET; cached copy still valid |
| 400 Bad Request | malformed syntax |
| 401 Unauthorized | missing/invalid authentication |
| 403 Forbidden | authenticated but not permitted |
| 404 Not Found | no such resource (or hidden) |
| 405 Method Not Allowed | wrong method (Allow header) |
| 409 Conflict | state conflict, duplicate, version mismatch |
| 412 Precondition Failed | If-Match didn't match (optimistic locking) |
| 415 Unsupported Media Type | wrong Content-Type |
| 422 Unprocessable Content | well-formed but semantically invalid |
| 429 Too Many Requests | rate limited (Retry-After) |
| 500 Internal Server Error | unexpected failure |
| 502 Bad Gateway | bad response from upstream |
| 503 Service Unavailable | overloaded / maintenance (Retry-After) |
| 504 Gateway Timeout | upstream timed out |
Headers worth knowing
| Header | Purpose |
|---|---|
Host | which site (virtual hosting); required in HTTP/1.1 |
Content-Type, Content-Length, Transfer-Encoding: chunked | body format and framing |
Accept, Accept-Encoding, Accept-Language | content negotiation |
Authorization, WWW-Authenticate | credentials and challenges |
Cookie, Set-Cookie | session state |
Cache-Control, ETag, Last-Modified, If-None-Match, If-Match, Vary, Age | caching and concurrency |
Location | redirects, created resources |
Retry-After | when to retry (429/503) |
Connection: keep-alive, Upgrade | connection management, WebSockets |
X-Forwarded-For, Forwarded, X-Request-ID, traceparent | proxies and tracing |
Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options | security |
Range, Content-Range | partial downloads, video seeking, resumable transfers |
Connections
- HTTP/1.0: new TCP connection per request.
- HTTP/1.1 keep-alive: reuse the connection for sequential requests; pipelining exists but is broken by intermediaries.
- HTTP/2/3: one connection, many concurrent streams.
Caching
| Content | Recommended headers |
|---|---|
Fingerprinted static assets (app.3f9a.js) | Cache-Control: public, max-age=31536000, immutable |
| HTML pages | Cache-Control: no-cache + ETag (always revalidate) |
| Public API data that may be slightly stale | Cache-Control: public, max-age=60, stale-while-revalidate=300 |
| User-specific responses | Cache-Control: private, max-age=0 or no-store |
| Secrets (tokens, PII) | Cache-Control: no-store |
Cookies
HTTP
Set-Cookie: __Host-sid=8f1c...; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=86400
| Attribute | Effect |
|---|---|
HttpOnly | invisible to JavaScript |
Secure | HTTPS only |
SameSite=Lax/Strict/None | cross-site sending rules (CSRF defence) |
Domain, Path | scope |
Max-Age / Expires | lifetime |
Partitioned (CHIPS) | third-party cookies partitioned per top-level site |
CORS
Content negotiation and compression
Accept: application/json↔Content-Type: application/json.Accept-Encoding: br, gzip, zstd↔Content-Encoding: br(Brotli/zstd beat gzip for text).Accept-Language: en-IN, en;q=0.8with quality values.- Always send
Varyfor headers that change the response.
HTTP/1.1 vs HTTP/2 vs HTTP/3
| HTTP/1.1 | HTTP/2 | HTTP/3 | |
|---|---|---|---|
| Transport | TCP | TCP | QUIC (UDP) |
| Format | text | binary frames | binary frames |
| Concurrency per connection | 1 request at a time | many streams | many independent streams |
| Header compression | none | HPACK | QPACK |
| HOL blocking | HTTP + TCP level | TCP level only | none at transport level |
| Handshake (with TLS 1.3) | 2 RTT | 2 RTT | 1 RTT / 0-RTT |
| Connection migration | ✗ | ✗ | ✓ |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.