Introduction to Transfer-Encoding feature in HTTP/1.1

http 1 1 transfer encoding rfc 9112
10 November 2024

In the HTTP/1.1 protocol, one of the very important and useful capabilities is the topic of Transfer-Encoding. This capability allows servers and proxies to send data in chunks or as chunked transfer and thus manage and transfer data in a more efficient and practical manner.

When using Transfer-Encoding, we can send large files without needing to calculate their exact size before sending them. This feature is especially useful when the content length is not previously specified, such as when using streaming data for input/output.

For instance, assume there is an API that receives a video file from another service and needs to send this file to users in real time. Here, Transfer-Encoding helps to prevent any delay and allows data to be sent more quickly.

One interesting point is that although the header related to Transfer-Encoding can include multiple sections, the structure remains chunked. However, utilizing this mode entails that clients should also have appropriate downstream handling capabilities.

Following is a simple example of how to send a response in chunked format that indicates which stages and executions are being processed.


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

7
Mozilla
9
Developer
7
Network
0

HTTP/1.1 200 OK
Indicates that the request was successful.

Content-Type: text/plain
The type of content sent. This is of text type.

Transfer-Encoding: chunked
This header specifies that data is being sent in a chunked format.

7
Size of the first chunk.

Mozilla
Data belonging to the first chunk.

9
Size of the second chunk.

Developer
Data belonging to the second chunk.

7
Size of the third chunk.

Network
Data belonging to the third chunk.

0
The end of the data has been sent, and no further data exists after this.

FAQ

?

Why should we use Transfer-Encoding?

?

How can we determine that the server is using chunked encoding?

?

What happens if the client doesn't support chunked encoding?