Chunked transfer encoding is an HTTP/1.1 feature that allows servers to stream data incrementally without knowing the total response size upfront. It's useful for streaming APIs, live updates, and large or dynamically-generated responses. In chunked transfer encoding, HTTP responses are modified into a series of chunks, each prefixed with its size in bytes. The client receives data incrementally and knows the response has ended when a zero-length chunk appears. A hands-on example using FastAPI and curl demonstrates how chunked transfer encoding works in practice. The example shows how each chunk starts with its length in hexadecimal, followed by the data, and the next chunk starts after a newline. The response ends with a zero-length chunk, indicating the end of data transmission. Under the hood, the Starlette framework receives yielded data segments and pushes each chunk to the underlying ASGI server, which formats it according to the HTTP/1.1 chunked transfer encoding specification. HTTP/2 and HTTP/3 do not use chunked encoding, instead using a more efficient binary framing layer that allows multiplexing multiple streams over a single connection. Understanding chunked transfer encoding helps build better streaming APIs and debug complex HTTP interactions effectively.
dev.to
dev.to
Create attached notes ...
