How to send an HTTP response in chunks? Chunked Transfer Encoding

HTTP provides (RFC 9112) a way for sending several chunks back by a server as a response to the request. Let’s see how to communication looks like.

Communication

A client sends a normal request, like for example:

GET /resource HTTP/1.1
Host: example.com
Accept: text/plain

And a server responds in several chunks like:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/plain

4
Wiki

Response structure

Each chunk contains at least two parts:
– size of a chunk
– new line character (CRLF)
– a chunk content
– new line character (CRLF)

Last chunk

The last chunk always contains:
– 0
– new line character (CRLF)
– new line character (CRLF)

Worth taking a look

1. https://datatracker.ietf.org/doc/html/rfc9112#section-7.1

Pozostaw komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *