Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active December 2, 2016 09:25
Show Gist options
  • Save nepsilon/3c5e2c1c5c2fece59a809217a66b8ea3 to your computer and use it in GitHub Desktop.
Save nepsilon/3c5e2c1c5c2fece59a809217a66b8ea3 to your computer and use it in GitHub Desktop.
Understanding HTTP’s Transfer-Encoding: chunked — First published in fullweb.io issue #74

Understanding HTTP’s Transfer-Encoding: chunked

Simply put, Transfer-Encoding: chunked, in a HTTP response header, tells us the body will be received in several chunks.

This is ideal in 2 scenarios:

  1. When the size of the body isn’t known in advance, like when generating output from a database.
  2. When the size of the body is too big to be fully loaded in RAM before being sent to the client.

In practice, the body has each of its chunks separated with X\r\n, where X is the size of the chunk in hexadecimal. For instance:

Transfer-Encoding: chunked

7\r\n
Fullweb\r\n
E\r\n
Fullstack news\r\n
0\r\n
\r\n

\r\n on a new line not prefixed with a size are seen as regular new line characters. The size does not account for the trailing \r\n. The last chunk is always of size 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment