The length problem
Normally a response carries a Content Length header so the client knows when the body ends. But sometimes the server generates the body on the fly and cannot know the size in advance. Chunked transfer encoding solves this by sending the body in pieces.
How chunks work
With Transfer Encoding chunked, the server omits Content Length and instead sends a sequence of chunks. Each chunk begins with its size written in hexadecimal, then the data, then a line break. A final chunk of size zero marks the end, optionally followed by trailer headers.
Why it matters
- It enables streaming so the client processes data as it arrives rather than waiting.
- It supports trailers, headers computed only after the body, such as a content hash.
- It lets a long running response begin before generation finishes.
This is an HTTP version one feature. HTTP version two and three frame data natively, so chunked encoding is not used there; the protocol layer handles streaming instead.
Key idea
Chunked transfer encoding streams a body as size prefixed pieces ending in a zero chunk, letting a server respond before it knows the total length.