← Lessons

quiz vs the machine

Gold1420

Networking

Chunked Transfer Encoding

How a server streams a body without knowing its total length up front.

4 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What marks the end of a chunked body?

2. Why use chunked encoding instead of Content Length?