What this page is for

Intent: the full picture behind the quick HTTP message page. Same sections, same order, with more depth in each: raw bytes, exact rules, full tables, commands and the harder questions.
Read the quick page first. Each section here starts where the quick one stops.
Time: about 20 minutes.

Words you need first

Everything from the quick page (HTTP, request, response, method, status code, header, body, safe, idempotent, cookie, stateless), plus:

  • Request target: the part after the method. For GET /cart?page=2 HTTP/1.1 it is /cart?page=2. The ? starts the query string, a set of name=value pairs the server can read.
  • Protocol version: the last word of the start line, such as HTTP/1.1. It says which HTTP rules the message follows.
  • CRLF: the two characters carriage return and line feed (\r\n). HTTP/1.1 ends every start line and header line with CRLF.
  • Reason phrase: the text after the status code, e.g. OK. It is free text for humans. Clients must read the number, not the words.
  • Field name: the name before the colon in a header. Field names are case-insensitive, so content-type and Content-Type are the same.
  • Framing: how the reader knows where the body ends. Either Content-Length (a byte count) or chunked transfer coding (the body is sent in pieces, each with its size).
  • Location header: a response header with a URL. Used with 201 and the 3xx redirects.
  • Pseudo-header: in HTTP/2 and HTTP/3, a header with a leading colon, such as :method. It carries the request line data.
  • RFC (Request for Comments): an official Internet standard document. HTTP is defined in RFC 9110 (semantics) and RFC 9112 (HTTP/1.1 message syntax).

Why it exists

The quick version: a browser and a server need one shared format. The detailed version adds two things the format solves. First, it lets many different programs interoperate, because the rules are written down and every field has a meaning. Second, it lets a message be read in a stream, one line at a time, without knowing the whole size first. The blank line and the length rules make that possible.

The picture

HTTP message: a request above a response, each made of start line, headers, blank line and body

The same picture as the quick page. The parts it labels are the parts the rest of this page takes apart: the start line in each message, the header lines, the empty line, and the body with its length.

How it works, with an example

Simply: Asking over HTTP is like sending a paper order slip. The browser writes a request with a method, a path and headers, and the server writes back a status such as 200 OK with the answer.

What to know: The same shop.com → api.com example, as the raw bytes on the wire.

Request (the browser writes it, your page code cannot change Host or Origin). There’s no Cookie line: a cross-origin fetch sends cookies only with credentials: "include" (see CORS):

GET /cart HTTP/1.1
Host: api.com
Origin: https://shop.com
Accept: application/json
 

The final empty line is the blank line. The request has no body, so nothing follows it.

Response:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 15
Cache-Control: no-store
 
{"items": [42]}

The body is exactly 15 bytes. Check it yourself with printf '%s' '{"items": [42]}' | wc -c. A Content-Length that does not match the real body is a classic bug. If it is too small, the client cuts the body short. If it is too big, the client waits for bytes that never come.

Parsing rules (RFC 9112):

  • The start line is read first. For a request it is method SP request-target SP version, where SP is a single space.
  • Header lines follow, each field-name: value, and end with CRLF. Optional spaces around the value are ignored.
  • A line with only CRLF ends the headers. Then the body starts.
  • If there is no Content-Length and no Transfer-Encoding, a request has no body. A response without framing is read until the connection closes.

Chunked transfer coding. When the server does not know the size up front, it sends Transfer-Encoding: chunked. Each chunk is a hex size, CRLF, the bytes, CRLF. A zero-size chunk ends the body:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
 
4
Wiki
5
pedia
0
 

That body is Wikipedia: 4 bytes, then 5 bytes, then the end.

HTTP/1.1 vs HTTP/2 vs HTTP/3

  • HTTP/1.1 is the text format above. Replies come back in order on a connection, so a slow reply holds up the ones behind it. Clients open several connections to work around this. It runs over TCP.
  • HTTP/2 keeps the same methods, status codes, headers and body rules. The message is split into binary frames, and many requests share one TCP connection as streams. Header names are always lowercase on the wire. The start line becomes pseudo-headers: :method: GET, :path: /cart, :authority: api.com, :scheme: https.
  • HTTP/3 does the same over QUIC, which runs over UDP instead of TCP. It removes the TCP-level wait that can block all streams when one packet is lost.

Application code and most headers do not change between versions. DevTools still shows the familiar text form.

The most common mistake

Length mistakes and assuming the body is always there

The quick page’s warning still holds, plus three details:

  • Wrong Content-Length. Count bytes, not characters. A non-ASCII letter like é is two bytes in UTF-8.
  • Missing blank line. Without the empty line the reader keeps waiting for headers, and the body is never found.
  • Body on a GET. RFC 9110 gives a body on GET no defined meaning. Many servers and proxies drop it. Send data on GET in the query string, or use POST.

Methods

Simply: A method is the verb of the request, like GET to read or POST to create. A safe method changes nothing. An idempotent method has the same effect on the server whether you send it once or many times, like a lift button that still calls one lift.

What to know:

Exact definitions (RFC 9110, section 9):

  • A safe method is one the client does not ask the server to change anything for. Safe methods are GET, HEAD, OPTIONS and TRACE.
  • An idempotent method is one where sending the request N times has the same effect on server state as sending it once. Idempotent methods are GET, HEAD, OPTIONS, TRACE, PUT and DELETE. All safe methods are idempotent.
  • Idempotence is about state, not about the response. A DELETE that returns 204 the first time and 404 the second time is still idempotent. The item is gone both times.
MethodBody in requestSafeIdempotentWhat the server does
GETNo defined meaning, avoidYesYesReturns a representation of the resource
HEADNoYesYesSame as GET, but sends headers only, no body
OPTIONSOptionalYesYesSays which methods are allowed. Use the Allow header in the reply
POSTYesNoNoProcesses the body, often creates a new resource
PUTYesNoYesReplaces the target resource with the body
PATCHYesNoNot guaranteedApplies a partial change (RFC 5789). Format is set by the media type, e.g. JSON Patch
DELETEOptionalNoYesRemoves the target resource

Plain rules to remember:

  • GET and HEAD must not change state. Caches and crawlers rely on this.
  • POST is the catch-all. Sending it twice can create two orders. Payment APIs fix this with an Idempotency-Key header: the server remembers the key and returns the first result on a repeat.
  • PUT is the full replacement. If you omit a field, the server should treat it as removed or reset. That is why PUT needs the whole resource.
  • PATCH sends a partial change. The change “add 1 to the count” is not idempotent: sending it twice adds 2. A change “set the name to Sam” is idempotent. So PATCH is not guaranteed either way.

Commands to try:

curl -X PUT -H "Content-Type: application/json" -d '{"name":"Sam"}' https://api.com/profile
curl -I https://api.com/cart    # sends HEAD, shows headers only
curl -X OPTIONS -i https://api.com/cart

Status codes

Simply: Every reply has a three-digit status code, like a note from a kitchen. The first digit is the family: 2 means it worked, 3 means go elsewhere, 4 means your request is wrong, and 5 means the server failed.

What to know:

The five families (RFC 9110, section 15):

FamilyMeaningClient should
1xxInformational. Interim reply, final reply still comingKeep waiting. 100 Continue lets a client send a body after asking
2xxSuccessRead the body if there is one
3xxRedirect or use a cached copyFollow the Location URL, or reuse the saved copy
4xxClient error: the request is wrong or not allowedChange the request for most 4xx. 408 and 429 are the exceptions: retry those unchanged after a wait (Retry-After or backoff)
5xxServer error: the server failedRetry later is reasonable, often with backoff

Common codes in detail:

CodeNameDetails
200OKThe body holds the answer
201CreatedA new resource exists. Location gives its URL
202AcceptedTaken, but not finished. Work happens later
204No ContentSuccess, and no body on purpose
301Moved PermanentlyClients and caches may remember the new Location
302FoundTemporary move. Browsers often change a POST to GET on a follow-up
303See OtherAlways follow with GET. Common after a POST: “go look at the result here”
304Not ModifiedReply to a conditional request (If-None-Match, If-Modified-Since). No body. See [[networking/http-caching
307Temporary RedirectLike 302, but the method and body are kept
308Permanent RedirectLike 301, but the method and body are kept
400Bad RequestMalformed syntax, e.g. bad JSON
401UnauthorizedNo valid credentials. The reply must include WWW-Authenticate
403ForbiddenCredentials are fine, but this action is not allowed. Logging in again does not help
404Not FoundNo resource here. Also used to hide that a resource exists
405Method Not AllowedThe URL exists, but not for this method. The reply must include an Allow header
409ConflictThe change clashes with current state, e.g. a duplicate username
422Unprocessable ContentWell-formed JSON with wrong values, e.g. a bad email. Many APIs use it for validation
429Too Many RequestsRate limit. Often sent with Retry-After (seconds or a date)
500Internal Server ErrorGeneric server bug
502Bad GatewayA proxy got an invalid reply from the server behind it
503Service UnavailableOverloaded or down. Can include Retry-After
504Gateway TimeoutA proxy waited too long for the server behind it

401 vs 403, precisely. 401 is about authentication (who are you?). The reply must include WWW-Authenticate to say how to log in. 403 is about authorization (are you allowed?). The server knows the caller and refuses. A common trap: returning 404 instead of 403 on purpose, so an attacker cannot tell that a private resource exists.

Commands:

curl -i https://api.com/cart          # shows the status line and headers
curl -o /dev/null -s -w '%{http_code}\n' https://api.com/cart   # prints just the code

Stateless and cookies

Simply: The server forgets each request once it answers, so sites use a cookie to remember you. A cookie is a small value the browser stores and sends back each time, like a coat-check ticket.

What to know:

Stateless means the server keeps no memory of earlier requests between messages. Each request must carry everything the server needs: the method, the path, the credentials. This makes it easy to put many servers behind a load balancer, because any server can answer any request.

Cookies add state on top, using two headers.

  1. The server replies with Set-Cookie: session=abc123; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=3600.
  2. The browser stores it for the matching domain and path.
  3. On later requests that match, the browser adds Cookie: session=abc123 by itself.

What the attributes do:

  • Path and Domain limit where the browser sends the cookie.
  • Max-Age or Expires sets how long it lives. Without either, it is a session cookie, deleted when the browser closes.
  • HttpOnly hides it from page JavaScript (document.cookie). It reduces theft by injected scripts.
  • Secure sends it only over HTTPS.
  • SameSite controls cross-site sending. Lax is the default in current Chrome when no value is set. See CORS for the cross-origin side.

A common design: the cookie holds only a random session ID. The server stores the real data (who you are, your cart) in a database or cache keyed by that ID. Some APIs skip cookies and send Authorization: Bearer <token> on each request. The page code must add that header itself, so the browser does not send it automatically.

Say it in an interview

Start with the 60-second answer from the quick page. If they push deeper, add one of these:

  • Framing: “The reader needs the blank line to find the end of the headers, and Content-Length or chunked encoding to find the end of the body. A mismatch breaks the next message on the connection.”
  • Idempotency in practice: “PUT and DELETE are safe to retry after a timeout because repeating them is harmless. POST is not, so payment APIs use an idempotency key.”
  • Stateless and scaling: “Because the server keeps no session state in memory, any instance can answer, which is why the state lives in a cookie, a token, or a shared store.”

Questions interviewers ask

The ten from the quick page, plus these harder ones. Tap to reveal.

Mental map

HTTP message mental map

The same map as the quick page. Each branch is a section above.

Watch

KobbyKernel, about 14 minutes: what a request and a response are made of, and persistent vs non-persistent connections.

Longer, hands-on:

Traversy Media, about 38 minutes: HTTP methods, headers and status codes, then real requests in the browser’s dev tools and Postman.