Skip to content
Sign inGet all access
Chapter 5·20 minFree

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: close
OutputHTTP/1.1 200 OK

Most 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: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/html

A charset rides along, and a client needs it to decode text:

terminal
{ 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-type
OutputContent-Type: text/plain; charset=utf-8

A client states what it wants with Accept. A static server ignores it:

GET / HTTP/1.1
Host: localhost
Accept: application/json
Connection: close
OutputHTTP/1.1 200 OK

That 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: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615

The receiver counts and stops. Send eleven bytes, declare five:

terminal
{ 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/
Outputhello

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:

terminal
{ 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/
Outputhello world

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:

terminal
{ 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/
Output5hello6 world0

5 is hexadecimal, so ff announces 255 bytes:

terminal
{ 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 -c
Output255

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

terminal
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/
Outputhello hello hello hello

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:

terminal
nc -l 9000 &
curl -s --max-time 1 -d 'q=a+b' localhost:9000/f
OutputPOST /f HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Content-Length: 5Content-Type: application/x-www-form-urlencodedq=a+b

Escaping is the client’s job, and -d does not do it:

terminal
nc -l 9000 | tail -4 &
curl -s --max-time 1 -d 'name=ada lovelace' localhost:9000/f
OutputContent-Length: 17Content-Type: application/x-www-form-urlencodedname=ada lovelace

--data-urlencode does, turning the space into +:

terminal
nc -l 9000 | tail -4 &
curl -s --max-time 1 --data-urlencode 'name=ada lovelace' localhost:9000/f
OutputContent-Length: 17Content-Type: application/x-www-form-urlencodedname=ada+lovelace

multipart/form-data

A file cannot be key=value, so each field becomes a part with its own headers:

terminal
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/upload
OutputPOST /upload HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Content-Length: 304Content-Type: multipart/form-data; boundary=----BOUNDARY----BOUNDARYContent-Disposition: form-data; name="name"ada----BOUNDARYContent-Disposition: form-data; name="file"; filename="up.txt"Content-Type: text/plainhello----BOUNDARY--

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

terminal
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 9000
OutputPOST /upload HTTP/1.1Host: localhostContent-Type: multipart/form-data; boundary=XbXbContent-Length: 177Connection: close--XbXbContent-Disposition: form-data; name="field"value1--XbXbContent-Disposition: form-data; name="f"; filename="a.txt"Content-Type: text/plainhello--XbXb--

A real parser accepts it — Python’s email module reads both parts back:

terminal
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()))'
Outputform-data; name="field" -> 'value1'form-data; name="f"; filename="a.txt" -> 'hello'

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: close
OutputHTTP/1.1 200 OK

Trailers come after a chunked body, for values you cannot know in advance:

terminal
{ 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/
Outputhello