1. What Is It?
(HyperText Transfer Protocol) is the application-layer protocol that powers the web and the vast majority of API communication. It defines a simple request-response model: a client sends a request message, a server processes it, and the server sends back a response. Every REST API, every browser page load, and most microservice communication runs on .
The problem HTTP solves: applications need a standard way to exchange structured messages across a connection. HTTP defines message format, verbs (GET, POST, PUT, etc.), status codes, and headers — giving both sides a common language without needing custom framing logic.
A team is building a REST API that multiple client applications will consume. They want to avoid writing custom message-framing logic on both the client and server sides. Which characteristic of HTTP directly addresses this requirement?
2. How It Works
Request Anatomy
GET /api/users/42 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbG...
Accept: application/json
Connection: keep-alive
- Method — the verb:
GET(read),POST(create),PUT/PATCH(update),DELETE(remove),HEAD(headers only),OPTIONS( preflight). - Path — the resource identifier. Query string (
?key=value) is part of the path. - version —
/1.1(orHTTP/1.0). HTTP/2 has no text request line. It sends the method and path as binary pseudo-headers (:method,:path). - Headers — key-value metadata: authentication, content type, caching directives, encoding.
- Body — optional payload, typically JSON or form-encoded data (present in POST/PUT/PATCH, absent in GET/DELETE).
Response Anatomy
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 57
Cache-Control: max-age=60
{"id": 42, "name": "Alice", "email": "alice@example.com"}- Status line — protocol version + status code + reason phrase.
- Headers — server metadata: content type, length, caching, , cookies.
- Body — the payload.
Full Cycle
For a single https:// request, the network path is: lookup → handshake → handshake → HTTP request → response. This is 3–4 round trips before data arrives. HTTP persistent connections (Connection: ) reuse the + session for subsequent requests, eliminating the handshake overhead.
The HTTPS cycle, stage by stage
A new HTTPS connection using HTTP/1.1 resolves the server address, establishes TCP and TLS, then carries the HTTP request and response.
DNS resolves api dot example dot com so the client can reach the server.
Text version
- DNS resolves api dot example dot com so the client can reach the server.
- With the address resolved, the client starts a TCP handshake on port four hundred forty-three.
- The client completes TCP with an ACK and sends ClientHello to start the encrypted TLS channel.
- With the encrypted channel ready, the client sends a GET request for user forty-two.
- The server returns status two hundred OK with the response body.
A mobile app makes repeated API calls to the same HTTPS endpoint. After the first request completes successfully, subsequent requests arrive noticeably faster. Which mechanism is most responsible for this improvement?
3. What SDEs Actually Need to Know
Status codes by class:
| Range | Meaning | Common examples |
|---|---|---|
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirect | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 4xx | Client error | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests |
| 5xx | Server error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout |
The 401 vs 403 distinction:
401 Unauthorized— credentials are missing or invalid. "Who are you?"403 Forbidden— credentials are valid but the user lacks permission. "I know who you are, but you can't do this."
:
GET,PUT,DELETE,HEADare idempotent — calling them multiple times has the same effect as calling once. Retrying them is safe.POSTis not idempotent by default — retrying a POST may create duplicate records. Use keys (e.g., Stripe'sIdempotency-Keyheader) to make POST retries safe.
Headers SDEs interact with most:
Content-Type— declares the payload format (application/json,multipart/form-data, etc.). Mismatch causes 415 Unsupported Media Type or parsing failures.Authorization— carries credentials:Bearer <token>,Basic <base64>,AWS4-HMAC-SHA256 ....Cache-Control— controls caching behavior for CDNs and browsers.no-cache,no-store,max-age=N.X-Request-ID/X-Trace-ID— correlation IDs for tracing requests across services. Not standard but universal in production systems.
Connection management:
- /1.1 defaults to persistent connections (
). The server closes them afterkeepalive_timeout(default 75s in nginx, but the stocknginx.confsets 65s). - Sending
Connection: closeforces the server to close after the response — useful if you need guaranteed connection cleanup. - Keep-alive connections are pooled by the client library (for example
requests.Sessionor OkHttp), not by the OS. In high-throughput services,MAX_CONNECTIONSand connection pool sizing matter.
401 and 403 answer different questions
401 means credentials are missing or invalid; 403 means the server understands the request but refuses it.
Four hundred one means credentials are missing or invalid, while four hundred three does not prove valid credentials.
Text version
- Four hundred one means credentials are missing or invalid, while four hundred three does not prove valid credentials.
- Four hundred three means the server will not allow this request, so the same credentials again do not help.
A user is authenticated and logged in, but attempts to access an admin-only endpoint they don't have permission for. Which HTTP status code should the server return?
4. Tradeoffs & Decisions
method semantics in API design: Using methods correctly lets clients (and intermediaries like CDNs) make safe assumptions. GET requests can be cached; POST requests cannot. DELETE requests can be retried; POST requests typically cannot without keys. Violating these semantics — like using POST for all reads — breaks caching, monitoring, and client retry logic.
Chunked transfer encoding vs. Content-Length:
Content-Length— client knows the response size upfront, which is efficient. A server can stream a body of known size (a static file). But for generated content, it must buffer the entire response to compute it.Transfer-Encoding: chunked— server streams data in pieces without knowing the total size. Better for large responses (file downloads, streaming data). Intermediate proxies must handle chunked encoding properly.
Compression:
Accept-Encoding: gzip, br tells the server the client can handle compressed responses. JSON APIs can see 60–80% size reduction with gzip. The cost is CPU time for compression/decompression and slightly increased time-to-first-byte for small responses. Enable for API responses > ~1KB; disable for small responses and pre-compressed content (images, video).
Known length or streamed pieces
For HTTP/1.1 responses with a body, Content-Length declares content bytes upfront; chunked transfer needs no total length.
Content-Length gives the body byte count upfront, while chunked transfer needs no total length.
Text version
- Content-Length gives the body byte count upfront, while chunked transfer needs no total length.
- Chunked transfer sends pieces as they are ready, so the server can start before it knows the total size.
- To send Content-Length for generated output, the server may buffer it first, while chunked transfer needs proxies that handle chunks.
Your backend API returns a large JSON dataset (several megabytes) to a dashboard client. The server currently buffers the entire response before sending it. A teammate suggests switching to chunked transfer encoding. What is the primary advantage of making this change?
5. Interview Cheat Sheet
Key sentences:
- " is a request-response protocol over : client sends a message (method + path + headers + optional body), server responds (status + headers + body)."
- "Before a single byte of data flows over HTTPS, there's a lookup, a handshake, and a handshake — totaling 3–4 round trips. Persistent connections amortize this."
- "Status codes are grouped: 2xx success, 3xx redirect, 4xx client error, 5xx server error. 401 means unauthenticated; 403 means unauthorized."
- "GET/PUT/DELETE are idempotent; POST is not — retrying a POST without keys can create duplicate side effects."
Common follow-ups:
Q: What's the difference between PUT and PATCH? A: PUT replaces the entire resource — you send the full updated representation. PATCH applies a partial update — you send only the fields to change. PUT is idempotent and semantically cleaner for full replacements; PATCH is more efficient for partial updates on large resources.
Q: What happens when a browser sees a 301 vs. 302 redirect?
A: Both redirect the client to a new URL via the Location header. 301 (Moved Permanently) tells the browser to update bookmarks and send future requests directly to the new URL; the redirect is cached indefinitely. 302 (Found / Temporary Redirect) means the original URL is still authoritative; the browser follows the redirect but doesn't cache it.
Q: How does HTTP caching work?
A: The server sends Cache-Control headers that tell clients and intermediaries (CDNs, proxies) how long to cache a response. max-age=3600 means cache for 1 hour. ETag and Last-Modified enable conditional requests: the client sends If-None-Match or If-Modified-Since; if the resource hasn't changed, the server responds 304 Not Modified with no body, saving bandwidth.