What’s inside an HTTP request
Your framework hands you req.method, req.headers and req.body as if they arrived that way. They didn’t: they arrived as a few lines of text. Here is that text, and how a server cuts it into parts, byte by byte.
01 One request, as sent
Say a client creates a user by sending some JSON to /users. With curl that is one line:
-d makes curl send a body, and a request with a body defaults to POST. Over HTTP/1.1, this is what goes down the TCP connection:
That is the whole request. No hidden binary envelope, no special encoding: plain ASCII text with a body on the end. Your framework’s req object is built from exactly these bytes.
02 Four parts and two line breaks
Every HTTP/1.1 request has the same shape. Lines end in two bytes, carriage return and line feed, written \r\n (CRLF). An empty line separates the headers from the body.
| Part | Example | What it says |
|---|---|---|
| Method | POST | The action. GET reads, POST creates, PUT replaces, PATCH changes part, DELETE removes. |
| Target | /users?page=2 | Which resource: a path, plus an optional query string after ?. |
| Version | HTTP/1.1 | Which rules the rest of the message follows. |
| Headers | Content-Type: application/json | Facts about the request: the host, the body’s format and size, cookies, credentials. |
| Body | {"name":"Ana"} | The data itself. Often absent: a GET usually has none. |
03 How the server reads it
The server cannot know in advance how long the request is. So it parses in a fixed order: split the first line on spaces, then read header lines until an empty one, then use the headers to work out how much body to read. Follow it through our request.
- parsing now
- already parsed
- not read yet
- just stored
The server has the bytes in a buffer and has parsed nothing yet. The parser starts at byte 0 with an empty request object.
Header names are case-insensitive, so Content-Length and content-length mean the same. Node stores them lower-cased, which is why you read req.headers['content-type'].
04 Where the body ends
The connection usually stays open for more requests, so “read until the connection closes” does not work. The headers must say where the body stops. There are two ways.
0 ends the body.The same 14-byte body, sent as two chunks of 7 bytes. A request uses one of the two methods, never both.
05 See the raw bytes yourself
nc (netcat) can listen on a port and print whatever arrives, with no HTTP parsing at all. Start it in one terminal:
Send the request from a second terminal:
The first terminal prints the request exactly as it arrived. curl adds User-Agent and Accept on its own, and works out Content-Length for you. curl will hang waiting for a response that never comes; press Ctrl+C in either terminal.
HTTP/2 and HTTP/3 carry the same parts, but as compressed binary frames instead of text. The request line becomes the pseudo-headers :method, :path, :scheme and :authority, and header names are always lower-case. Your framework hides the difference, so req.method reads the same either way.
06 When the bytes don’t match
Content-Length: 20 (body is 14 bytes)A Content-Length that is too bigThe server waits for 6 more bytes that never arrive, and the request hangs until a timeout. Too small is worse: the leftover bytes are read as the start of the next request.curl -d '{"name":"Ana"}' …JSON sent without Content-Typecurl -d sends application/x-www-form-urlencoded by default. A JSON body parser such as express.json() checks the type and skips the body. Under Express 5, req.body is then undefined (Express 4 left an empty {}), so const { name } = req.body throws.GET /search?q=red mug HTTP/1.1A space in the targetSpaces split the request line, so the server finds four parts where it expects three and rejects the request, typically with 400. Encode the space as %20 (or + in a query string).printf 'GET / HTTP/1.1\nHost: x\n\n'Bare \n instead of \r\nThe spec says lines end in CRLF. Some servers accept a bare \n, others reject it. When you write a request by hand, use \r\n.GET / HTTP/1.1 (no Host line)No Host headerHTTP/1.1 requires Host, and the spec tells servers to answer 400 Bad Request without it. Clients add it for you; hand-written requests forget it.