{}DSA Atlas

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.

Intermediate8 interview questions

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

MethodPurposeSafeIdempotentBodyCacheable
GETread a resource✓✓no (semantically)✓
HEADheaders only✓✓no✓
OPTIONScapabilities, CORS preflight✓✓optional✗
POSTcreate / process / action✗✗yesrarely
PUTreplace (or create at a known URI)✗✓yes✗
PATCHpartial update✗✗ (can be made so)yes✗
DELETEremove✗✓optional✗

Status codes that matter

CodeUse it when
200 OKsuccess with a body
201 Createdresource created (Location header)
202 Acceptedwork queued; poll a status URL
204 No Contentsuccess, nothing to return (DELETE, PUT)
301 / 308permanent redirect (308 keeps method/body)
302 / 307temporary redirect (307 keeps method/body)
303 See Otherafter a POST, redirect to a GET
304 Not Modifiedconditional GET; cached copy still valid
400 Bad Requestmalformed syntax
401 Unauthorizedmissing/invalid authentication
403 Forbiddenauthenticated but not permitted
404 Not Foundno such resource (or hidden)
405 Method Not Allowedwrong method (Allow header)
409 Conflictstate conflict, duplicate, version mismatch
412 Precondition FailedIf-Match didn't match (optimistic locking)
415 Unsupported Media Typewrong Content-Type
422 Unprocessable Contentwell-formed but semantically invalid
429 Too Many Requestsrate limited (Retry-After)
500 Internal Server Errorunexpected failure
502 Bad Gatewaybad response from upstream
503 Service Unavailableoverloaded / maintenance (Retry-After)
504 Gateway Timeoutupstream timed out

Headers worth knowing

HeaderPurpose
Hostwhich site (virtual hosting); required in HTTP/1.1
Content-Type, Content-Length, Transfer-Encoding: chunkedbody format and framing
Accept, Accept-Encoding, Accept-Languagecontent negotiation
Authorization, WWW-Authenticatecredentials and challenges
Cookie, Set-Cookiesession state
Cache-Control, ETag, Last-Modified, If-None-Match, If-Match, Vary, Agecaching and concurrency
Locationredirects, created resources
Retry-Afterwhen to retry (429/503)
Connection: keep-alive, Upgradeconnection management, WebSockets
X-Forwarded-For, Forwarded, X-Request-ID, traceparentproxies and tracing
Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Optionssecurity
Range, Content-Rangepartial 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

Browser CDN Origin GET /logo.png miss → fetch 200 · Cache-Control: max-age=600 · ETag: "a1" … within 10 min: served from browser cache, no request at all … after expiry: GET · If-None-Match: "a1" 304 Not Modified (no body) → reuse, refresh freshness
Freshness avoids requests entirely; validation avoids re-downloading the body.
ContentRecommended headers
Fingerprinted static assets (app.3f9a.js)Cache-Control: public, max-age=31536000, immutable
HTML pagesCache-Control: no-cache + ETag (always revalidate)
Public API data that may be slightly staleCache-Control: public, max-age=60, stale-while-revalidate=300
User-specific responsesCache-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
AttributeEffect
HttpOnlyinvisible to JavaScript
SecureHTTPS only
SameSite=Lax/Strict/Nonecross-site sending rules (CSRF defence)
Domain, Pathscope
Max-Age / Expireslifetime
Partitioned (CHIPS)third-party cookies partitioned per top-level site

CORS

Browser on app.example.com api.example.com OPTIONS /orders · Origin · Access-Control-Request-Method: POST 204 · Allow-Origin: https://app.example.com · Allow-Headers POST /orders (actual request) 201 · Access-Control-Allow-Origin: https://app.example.com
The browser asks permission before sending a non-simple cross-origin request.

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.8 with quality values.
  • Always send Vary for headers that change the response.

HTTP/1.1 vs HTTP/2 vs HTTP/3

HTTP/1.1HTTP/2HTTP/3
TransportTCPTCPQUIC (UDP)
Formattextbinary framesbinary frames
Concurrency per connection1 request at a timemany streamsmany independent streams
Header compressionnoneHPACKQPACK
HOL blockingHTTP + TCP levelTCP level onlynone at transport level
Handshake (with TLS 1.3)2 RTT2 RTT1 RTT / 0-RTT
Connection migration✗✗✓

Interview questions

0 / 8 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.