Lesson 7: Chunking the message body

Contents
  • Reading assignment
  • Hello chunks
  • Exercise 1: Testing with curl --raw
  • Why chunk?
  • The chunkResponse function
  • Exercise 2: Applying the chunkResponse function
  • A tale of two chunks
  • Exercise 3: Testing the two-chunk response

In lesson 6, we wrote a function to set the Content-Length header on a response. This header tells the recipient how many bytes to expect in the message body. The Content-Length header field is one of two ways to let the client know when it’s time to stop waiting for more of the response to read. This lesson is about the second way: using the Transfer-Encoding: chunked header to specify that we are going to segment the message body in multiple smaller parts (“chunks”).

Remember that not all HTTP messages have a body.

  • If a message does have a body, then it must have either Content-Length or Transfer-Encoding: chunked (not both).
  • If a message does not have a body, then it must not have either of these headers.

The exercises in this lesson ask you to try things out using GHCi and curl; they do not require writing any code.

Sign up for access to the full page, plus the complete archive and all the latest content.