Part AThe protocol
Request and response, no memory
HTTP (HyperText Transfer Protocol) is a request/response protocol: the client sends a request, the server returns one response. It is stateless: each request stands alone, and the server does not remember the previous one. That is why cookies and tokens exist, to carry "who you are" on every request (section 6).
Anatomy of a URL
#summary) is never sent to the server; only the browser uses it.What a request and a response contain
Both have the same shape: a start line, headers, a blank line, and an optional body. Here is the Kadé app placing an order:
Request
POST /orders HTTP/1.1
Host: api.kade.lk
Authorization: Bearer eyJhbGci…
Content-Type: application/json
Content-Length: 58
{"items":[{"sku":"RICE-5KG","qty":2}],"slot":"evening"}Request line: method + path + version. Then headers, a blank line, the body.
Response
HTTP/1.1 201 Created
Content-Type: application/json
Location: /orders/1042
Cache-Control: no-store
Content-Length: 41
{"id":1042,"status":"confirmed","total":…}Status line: version + status code + reason. Then headers, a blank line, the body.
The body's size is given by Content-Length. When the server does not know the size in advance (streaming a CSV export, chapter 19), it uses Transfer-Encoding: chunked in HTTP/1.1 and sends the body in pieces, each with its own length.
Methods
The method states the request's intent. Two properties decide what is safe to retry automatically, which matters a lot when connections drop (chapter 21):
| # | Method | Intent | Kadé example | Safe? | Idempotent? |
|---|---|---|---|---|---|
| 1 | GET | Read something | GET /orders/1042 | yes | yes |
| 2 | POST | Create, or run an action | POST /orders | no | no |
| 3 | PUT | Replace a resource entirely | PUT /users/me/address | no | yes |
| 4 | PATCH | Change part of a resource | PATCH /orders/1042 {"slot":"morning"} | no | not guaranteed |
| 5 | DELETE | Remove | DELETE /cart/items/9 | no | yes |
| 6 | HEAD | Like GET, headers only | checking a file's size | yes | yes |
| 7 | OPTIONS | Ask what is allowed | the browser's CORS "preflight" before a cross-site call | yes | yes |
Status codes
The first digit gives the family. Learn the families, and API debugging gets much faster:
| Code | Meaning | When kade-api (or something in front of it) sends it |
|---|---|---|
| 2xx · success | ||
| 200 | OK | GET /orders/1042 worked |
| 201 | Created | POST /orders created order 1042 (with a Location header) |
| 204 | No Content | DELETE /cart/items/9 worked, nothing to return |
| 3xx · go somewhere else | ||
| 301 / 308 | Moved permanently | http://kade.lk → https://kade.lk (308 keeps the method) |
| 302 / 307 | Moved temporarily | After login, back to the page you came from |
| 304 | Not Modified | "Your cached copy is still good" (section 7) |
| 4xx · the client got something wrong | ||
| 400 | Bad Request | Broken JSON |
| 401 | Unauthorized | No token, or an expired one: "who are you?" |
| 403 | Forbidden | Valid token, but a shopper calling an admin endpoint: "I know you; not allowed" |
| 404 | Not Found | GET /orders/99999 |
| 409 | Conflict | Order already paid |
| 422 | Unprocessable | JSON is valid, but qty is −3 |
| 429 | Too Many Requests | Rate limit hit (remember the whole office shares one IP, chapter 10) |
| 5xx · the server side broke | ||
| 500 | Internal Server Error | An unhandled exception in kade-api |
| 502 | Bad Gateway | The load balancer got a bad or no reply from kade-api (the keep-alive race in chapter 21) |
| 503 | Service Unavailable | kade-api is down or overloaded; often with Retry-After |
| 504 | Gateway Timeout | The load balancer waited too long for kade-api |
Headers, and remembering who you are
Headers carry metadata. The ones you meet daily:
| Host | Which site. One IP can serve many sites (virtual hosting); the Host header picks one. Required in HTTP/1.1. |
| Content-Type / Accept | What the body is / what the client wants back (application/json, text/html …) |
| Authorization | Credentials, usually Bearer <token> |
| Cookie / Set-Cookie | The server sets a cookie; the browser sends it back on every later request |
| Cache-Control, ETag | How long a response may be reused (section 7) |
| Content-Encoding | Compression: gzip or br (Brotli). JSON often shrinks to a fifth. |
| Location | Where to go (redirects), or where a new resource is (201) |
| Access-Control-Allow-Origin | CORS: which other websites may call this API from a browser |
Because HTTP is stateless, identity travels with every request, in one of two ways:
Cookies
Set-Cookie: sid=8f2a91c; HttpOnly; Secure; SameSite=Lax; Max-Age=86400
The browser stores it and sends Cookie: sid=8f2a91c automatically. HttpOnly: JavaScript cannot read it. Secure: HTTPS only. SameSite: not sent on most cross-site requests (blocks CSRF).
Tokens
Authorization: Bearer eyJhbGciOi…
The app stores the token and adds the header itself. Common for mobile apps and APIs. Often a JWT: signed data the server can check without a database lookup.
Caching in one minute
Cache-Control: max-age=86400 on a product image lets the browser (and any CDN) reuse it for a day without asking. When it expires, the browser asks again with If-None-Match: "a1b2" (the ETag it got before). If nothing changed, kade-api answers 304 Not Modified with no body. Cache-Control: no-store on order details means "never keep this". Caching skips whole requests, which beats any protocol speed-up.
Part BThe versions
Same meaning, better delivery
Every version keeps the same methods, status codes and headers. Only the way messages are carried changes:
| # | Version | Year | Carried as | Connections | Transport |
|---|---|---|---|---|---|
| 1 | HTTP/1.0 | 1996 | text | a new TCP connection for every request | TCP |
| 2 | HTTP/1.1 | 1997 | text | reused (keep-alive); one request at a time each | TCP |
| 3 | HTTP/2 | 2015 | binary frames | one connection, many streams at once | TCP (+ TLS) |
| 4 | HTTP/3 | 2022 | binary frames | one connection, fully independent streams | QUIC over UDP (TLS 1.3 built in) |
HTTP/1.1: keep-alive and head-of-line blocking
HTTP/1.1 kept connections open so they could be reused (keep-alive, chapter 21), saving a handshake per request. But on one connection, requests are answered one at a time, in order: a slow response blocks everything behind it. This is head-of-line blocking at the HTTP level.
Browsers worked around it by opening up to 6 connections per host, each paying its own TCP and TLS handshake and slow start (chapter 20). Sites even split assets across several host names to get more connections ("domain sharding"). An idea to send several requests without waiting (pipelining) existed, but was too buggy and is not used.
HTTP/2: many streams on one connection
HTTP/2 introduced multiplexing: many requests and responses share one connection as independent streams, cut into small binary frames and interleaved. Nothing waits in line at the HTTP level.
one TCP connection, frames from three streams interleaved
- Binary, not text: faster and less error-prone to parse.
- Header compression (HPACK): headers repeat on every request (cookies, user agent). HPACK sends them once and then refers to them by number.
- Per-stream flow control and priorities: the same idea as chapter 19, per stream.
- Server push (sending files before they are asked for) was part of HTTP/2 but proved unhelpful, and browsers have removed it.
The problem HTTP/2 could not fix
HTTP/2 still runs on TCP, and TCP delivers one ordered byte stream (chapter 17). If a single packet is lost, TCP holds back everything behind it until the resend arrives, including frames of streams that had nothing to do with the lost packet. HTTP/2 removed head-of-line blocking at the HTTP level but not at the TCP level. On lossy mobile networks, it can even do worse than HTTP/1.1's six connections, where one loss stalls only one of them.
HTTP/3 on QUIC
HTTP/3 runs on QUIC, a transport built on UDP (chapter 14) that does TCP's job itself, differently:
| # | Feature | What it gives |
|---|---|---|
| 1 | Independent streams | Each stream has its own ordering. A lost packet delays only the stream it belonged to. |
| 2 | TLS 1.3 built in | Transport and encryption handshakes are one: a new connection is ready in 1 round trip instead of 2 (chapter 25). A returning client can even send data in the first flight ("0-RTT"). |
| 3 | Connection IDs | A connection is identified by an ID, not by the four addresses and ports. When the delivery rider's phone moves from WiFi to mobile data (new IP), the connection keeps going instead of breaking. |
| 4 | Encrypted transport headers | Even packet numbers are encrypted, so middleboxes cannot interfere with it the way they sometimes do with TCP. |
| 5 | QPACK | Header compression that works with out-of-order streams |
Alt-Svc header on an earlier response, or from a DNS HTTPS record. They race QUIC against TCP and fall back to HTTP/2 if UDP 443 is blocked, as it is on some office networks.Watch it: loading the Kadé home page
The home page needs its HTML, then 18 more files (CSS, scripts, a font, product images). Compare how each version loads them from home (41 ms round trip), then lose one packet:
- connection setup (TCP + TLS)
- waiting for the first byte (1 RTT)
- downloading (c1-c6 = connection)
- ✕ lost packet · hatched = stalled
| Page fully loaded | no loss | one packet lost |
|---|---|---|
| HTTP/1.1 | 370 ms | 376 ms |
| HTTP/2 | 200 ms | 241 ms |
| HTTP/3 | 159 ms | 188 ms |
A simple model: 41 ms round trip, and a line fast enough that waiting, not bandwidth, is the limit. HTTP/1.1 opens 5 more connections once the HTML arrives, each paying 2 round trips of setup, then fetches one file at a time per connection. HTTP/2 and HTTP/3 ask for everything at once on one connection. With a loss, HTTP/2’s whole connection stalls for a round trip; HTTP/3 delays only milk.jpg; HTTP/1.1 delays only connection 4.
Versions are per connection
Each connection picks its own version, agreed during the TLS handshake (a field called ALPN, chapter 25). So one request can use several versions along the way:
- Shopper's browser
- Google load balancerHTTP/3 or HTTP/2
- kade-apiHTTP/1.1 or HTTP/2 inside GCP
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | HTTP | Request/response, stateless | stateless |
| 2-3 | Messages | URL parts; start line, headers, blank line, body | Content-Length, chunked |
| 4 | Methods | GET, POST, PUT, PATCH, DELETE; safe and idempotent decide retries | idempotency key |
| 5 | Status | 2xx ok, 3xx elsewhere, 4xx client, 5xx server (often a proxy) | |
| 6-7 | Headers | Host, auth, cookies with flags, tokens, caching with ETag/304 | virtual hosting, HttpOnly, SameSite, ETag |
| 9 | HTTP/1.1 | Keep-alive, one request at a time, 6 connections | head-of-line blocking |
| 10-11 | HTTP/2 | Binary streams multiplexed on one TCP connection; TCP still blocks on loss | multiplexing, frame, HPACK |
| 12 | HTTP/3 | QUIC on UDP: independent streams, 1-RTT setup, connection migration | QUIC, 0-RTT, Alt-Svc |
| 14 | Per hop | Each connection negotiates its own version | ALPN |
Try it yourself
The full request and response
- What to look for:
>lines are your request,<lines the response headers. - From section: 3. What a request and a response contain
curl -v https://example.comHeaders only (HEAD)
- What to look for:
set-cookie,cache-control,alt-svc: h3=":443". - From section: 6. Headers, and remembering who you are
curl -I https://www.google.comWhich version was used
- What to look for:
2; add--http1.1to force the old one. - From section: 14. Versions are per connection
curl -s -o /dev/null -w '%{http_version}\n' https://www.google.comAn HTTP/3 request
- What to look for:
HTTP/3 200. - Runs on: needs a curl built with HTTP/3
- From section: 12. HTTP/3 on QUIC
curl --http3 -I https://www.google.comThe version per request
- Steps: Chrome DevTools → Network → right-click the columns → Protocol
- What to look for:
h3,h2,http/1.1side by side; the Waterfall column shows waiting vs downloading. - From section: 13. Watch it: loading the Kadé home page
A conditional request
- What to look for:
304 Not Modifiedwhen the ETag still matches. - From section: 7. Caching in one minute
curl -I -H 'If-None-Match: "etag-value"' URL