Headers and Bodies
Framing, chunked encoding, form encoding and multipart uploads.
Headers are metadata. Framing the body is the interesting part.
Header syntax
Name: value, one per line, case-insensitive and order-independent. A repeated name is
legal and means a list:
GET / HTTP/1.1
Host: localhost
X-Tag: a
X-Tag: b
Connection: closeMost libraries hand you only the first value — where Set-Cookie bugs come from.
Content-Type
How to read the body. nginx picks it from the file extension:
GET /50x.html HTTP/1.1
Host: localhost
Connection: closeA charset rides along, and a client needs it to decode text:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Type: text/plain; charset=utf-8\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i content-typeA client states what it wants with Accept. A static server ignores it:
GET / HTTP/1.1
Host: localhost
Accept: application/json
Connection: closeThat 200 is HTML, not JSON. An API would answer 406 Not Acceptable — negotiation is an
application promise, not a protocol one.
Content-Length
The simplest framing: a byte count:
GET / HTTP/1.1
Host: localhost
Connection: closeThe receiver counts and stops. Send eleven bytes, declare five:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Length: 5\r\n'
printf 'Connection: close\r\n\r\n'
printf 'hello world'
} | nc -l 9000 >/dev/null &
curl -s localhost:9000/Chunked transfer encoding
When the length is not known in advance, send it in pieces: a size in hex, the data, and a zero-size chunk to finish:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Transfer-Encoding: chunked\r\n'
printf 'Connection: close\r\n\r\n'
printf '5\r\nhello\r\n'
printf '6\r\n world\r\n'
printf '0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -s localhost:9000/Both chunks reassembled, and no Content-Length anywhere. Chapter 8 uses this for a body
that never ends.
Reading chunks by hand
--raw stops curl decoding, so the framing shows:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Transfer-Encoding: chunked\r\n'
printf 'Connection: close\r\n\r\n'
printf '5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -s --raw localhost:9000/5 is hexadecimal, so ff announces 255 bytes:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Transfer-Encoding: chunked\r\n'
printf 'Connection: close\r\n\r\n'
printf 'ff\r\n'; python3 -c "print('x'*255, end='')"; printf '\r\n'
printf '0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -s localhost:9000/ | wc -cThe sizes count data only, not the \r\n around each chunk.
Content-Encoding
Content-Encoding is what the body is. Transfer-Encoding is how it was sent:
| Header | Meaning | Undone by |
|---|---|---|
Content-Encoding: gzip |
the resource is compressed | the client, after receiving |
Transfer-Encoding: chunked |
the transfer was framed | the connection, on arrival |
--compressed tells curl to accept gzip and undo it:
printf 'hello hello hello hello' | gzip > g.gz
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Content-Encoding: gzip\r\n'
printf 'Connection: close\r\n\r\n'
cat g.gz
} | nc -l 9000 >/dev/null &
curl -s --compressed localhost:9000/Without it, the same request yields 28 raw bytes.
Form encoding
application/x-www-form-urlencoded is key=value&key=value. curl -d sets it:
nc -l 9000 &
curl -s --max-time 1 -d 'q=a+b' localhost:9000/fEscaping is the client’s job, and -d does not do it:
nc -l 9000 | tail -4 &
curl -s --max-time 1 -d 'name=ada lovelace' localhost:9000/f--data-urlencode does, turning the space into +:
nc -l 9000 | tail -4 &
curl -s --max-time 1 --data-urlencode 'name=ada lovelace' localhost:9000/fmultipart/form-data
A file cannot be key=value, so each field becomes a part with its own headers:
echo hello > up.txt
nc -l 9000 | sed 's/-\{10,\}[A-Za-z0-9]*/----BOUNDARY/' &
curl -s --max-time 1 -F 'name=ada' -F '[email protected]' localhost:9000/uploadThree things make it work: the boundary in Content-Type, a Content-Disposition per
part, and --boundary-- to close.
A filename turns a field into a file, and only then does the part get its own
Content-Type.
Writing multipart by hand
It is text, so you can type it. The body is 177 bytes:
nc -l 9000 > hand.txt &
{ printf '%s\r\n' 'POST /upload HTTP/1.1' 'Host: localhost' \
'Content-Type: multipart/form-data; boundary=XbXb' \
'Content-Length: 177' 'Connection: close' ''
printf -- '--XbXb\r\n'
printf 'Content-Disposition: form-data; name="field"\r\n\r\nvalue1\r\n'
printf -- '--XbXb\r\n'
printf 'Content-Disposition: form-data; name="f"; filename="a.txt"\r\n'
printf 'Content-Type: text/plain\r\n\r\nhello\r\n'
printf -- '--XbXb--\r\n'
} | nc -w 1 localhost 9000A real parser accepts it — Python’s email module reads both parts back:
python3 -c '
import email, email.policy
hdr = b"Content-Type: multipart/form-data; boundary=XbXb\r\n\r\n"
body = open("hand.txt","rb").read().split(b"\r\n\r\n",1)[1]
msg = email.message_from_bytes(hdr+body, policy=email.policy.HTTP)
for p in msg.iter_parts():
print(p.get("Content-Disposition"), "->", repr(p.get_content()))'Custom headers and trailers
Anything the spec does not define is yours; X- was deprecated in 2012:
GET / HTTP/1.1
Host: localhost
Request-Id: 7f3a
Connection: closeTrailers come after a chunked body, for values you cannot know in advance:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Transfer-Encoding: chunked\r\n'
printf 'Trailer: X-Checksum\r\n'
printf 'Connection: close\r\n\r\n'
printf '5\r\nhello\r\n'
printf '0\r\nX-Checksum: abc123\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -s localhost:9000/