Skip to content

What’s inside an HTTP request

BasicsUnder the hood

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:

Shell
curl -H 'Content-Type: application/json' \
-d '{"name":"Ana"}' \
http://localhost:3000/users

-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:

HTTP
POST /users HTTP/1.1
Host: localhost:3000
User-Agent: curl/8.7.1
Accept: */*
Content-Type: application/json
Content-Length: 14
{"name":"Ana"}

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.

Text
POST /users HTTP/1.1\r\n ← request line: method, target, version
Host: localhost:3000\r\n ← headers: one per line, Name: value
Content-Type: application/json\r\n
Content-Length: 14\r\n
\r\n ← empty line: headers are over
{"name":"Ana"} ← body: exactly 14 bytes
What each part tells the server
PartExampleWhat it says
MethodPOSTThe action. GET reads, POST creates, PUT replaces, PATCH changes part, DELETE removes.
Target/users?page=2Which resource: a path, plus an optional query string after ?.
VersionHTTP/1.1Which rules the rest of the message follows.
HeadersContent-Type: application/jsonFacts 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.

A server parsing one HTTP/1.1 request
Step 1 of 9
Bytes on the connection (␍␊ is \r\n)
parserPOST
␠
/users
␠
HTTP/1.1
␍␊
Host: localhost:3000
␍␊
Content-Type: application/json
␍␊
Content-Length: 14
␍␊
␍␊
{"name":"Ana"}
Parsed request
Empty
  • 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.

request line reading

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.

Content-LengthSize up frontThe client knows the body’s size before sending, e.g. a JSON object it has already built. The server reads exactly that many bytes.
Transfer-Encoding: chunkedSize per chunkThe client streams data whose total size it doesn’t know yet. Each chunk starts with its size in hex; a chunk of size 0 ends the body.
Text
POST /users HTTP/1.1\r\n
Host: localhost:3000\r\n
Transfer-Encoding: chunked\r\n
\r\n
7\r\n
{"name"\r\n
7\r\n
:"Ana"}\r\n
0\r\n
\r\n

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:

Shell
nc -l 8080 # macOS; on most Linux builds: nc -l -p 8080

Send the request from a second terminal:

Shell
curl -H 'Content-Type: application/json' -d '{"name":"Ana"}' http://localhost:8080/users

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
POST /users HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.7.1
Accept: */*
Content-Type: application/json
Content-Length: 14
{"name":"Ana"}

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.

07 Check yourself

Question

How does an HTTP/1.1 server know the headers have ended?

Question

A request has Content-Length: 14 and a 14-byte body, on a connection that stays open. How does the server know where the body stops?

Question

You send JSON with curl -d but no -H 'Content-Type: application/json'. Your Express app uses express.json(). What is in req.body?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?