Write an HTTP/1.1 message by hand and follow the gudang reading it byte by byte: what happens when the blank line is missing, Content-Length comes from…
Why it matters
Every fetch() and every server reply turns into bytes of this shape down below. Know the shape and you read curl -v and the Network tab fast, and know why a request hangs with no blank line, why a wrong Content-Length cuts a body, and why one space before a colon makes a 400.
The key idea
The blank line closes the headers; Content-Length closes the body
Common mistakes
Thinking headers and body are split by a space or a special mark The split is one blank line: the ␍␊ at the end of the last header, then ␍␊ again. Without it the header section never ends, and the recipient waits out its limit (408).
Content-Length set to the character count, not the bytes A recipient reads bytes. str.length counts UTF-16 units, so a body with é or → is cut, and the rest is read as the next message. Count with new TextEncoder().encode(s).length, or let fetch write it.
Ending lines with \n alone HTTP/1.1 asks for ␍␊ (\r\n). Many servers forgive a lone ␊, since RFC 9112 allows it, but strict ones refuse with 400. Write \r\n.
A space between a header’s name and its colonContent-Type : … is no valid header. A server must refuse it with 400 (RFC 9112 §5.1): gaps like this were once used to smuggle requests. The name touches the colon; spaces may follow it.
Try it in this playground
Switch to a reply The start line becomes a status line, and the browser reads: the code gets a real Response.
Turn the blank line off The headers never end: the gudang waits, then 408.
Count Content-Length with .length on a body with é The body is cut mid-character: JSON.parse throws a SyntaxError.
Send a lone ␊ to a strict gudang It refuses at the first line: 400.