HTTP
Hypertext Transfer Protocol
The request and response language of the web.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
HTTP is a simple conversation: the client sends a request naming a method and a resource, and the server returns a status code, headers and usually a body. Every web page, API call and file download follows this pattern.
HTTP/1.1 is plain text. HTTP/2 sends the same ideas as binary frames multiplexed on one connection, and HTTP/3 runs them over QUIC. The meaning of methods, status codes and headers stays the same.
1.2Key ideas
The handful of ideas everything else follows from.
Methods
GET reads, POST creates or submits, PUT replaces, PATCH changes, DELETE removes. GET, PUT and DELETE are meant to be idempotent.
Status classes
1xx info, 2xx success, 3xx redirect, 4xx client mistake, 5xx server failure.
Caching
Cache-Control, ETag and Last-Modified let clients and CDNs reuse responses safely.
Statelessness
Each request stands alone. Cookies and tokens carry session state.
Evolution
HTTP/2 multiplexes requests and compresses headers. HTTP/3 removes TCP head-of-line blocking.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- What is the difference between 301 and 302?
- Why is Host required in HTTP/1.1?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
Method, path, version and headers such as Host.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
HTTP/1.1 is plain text over a TCP connection. Every line ends in CR LF (0d 0a); a blank line ends the headers.
Method, target and version. GET asks for a representation and must not change state.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Request line | 0 | 26 | 47 45 54 20 2f 69 6e 64 … | |
| Host | 26 | 19 | 48 6f 73 74 3a 20 65 78 … | |
| User-Agent | 45 | 24 | 55 73 65 72 2d 41 67 65 … | |
| Accept | 69 | 13 | 41 63 63 65 70 74 3a 20 … | |
| End of headers | 82 | 2 | 0d 0a |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Parsing an HTTP/1.1 request
HTTP/1.1 is plain text, and this parses the request on this page.
1export interface HttpRequest {2 method: string;3 target: string;4 version: string;5 headers: Map<string, string>;6 body: string;7}8 9/** Lines end in CRLF, and a blank line ends the headers. */10export function parseRequest(text: string): HttpRequest {11 const split = text.indexOf("\r\n\r\n");12 if (split === -1) throw new Error("incomplete headers");13 const [requestLine, ...lines] = text.slice(0, split).split("\r\n");14 const [method, target, version] = requestLine.split(" ");15 const headers = new Map<string, string>();16 for (const line of lines) {17 const colon = line.indexOf(":");18 headers.set(line.slice(0, colon).toLowerCase(), line.slice(colon + 1).trim()); // names ignore case19 }20 const length = Number(headers.get("content-length") ?? 0);21 return { method, target, version, headers, body: text.slice(split + 4, split + 4 + length) };22}23 24/** A client may reuse the connection unless someone said otherwise. */25export function keepAlive(r: HttpRequest): boolean {26 const connection = r.headers.get("connection")?.toLowerCase();27 return r.version === "HTTP/1.1" ? connection !== "close" : connection === "keep-alive";28}text.indexOf("\r\n\r\n")- A blank line ends the headers.
toLowerCase()- Header names ignore case, and a parser that forgets this breaks in strange ways.
content-length- Says exactly where the body ends, which allows several requests on one connection.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
What is the difference between 301 and 302?
Why is Host required in HTTP/1.1?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Plain HTTP is readable and modifiable by anyone on the path. Use HTTPS.
- A missing Content-Length or chunked encoding mistake leaves the client waiting forever.
- A retried POST can create duplicates. Use idempotency keys.