Message Body Length in HTTP/1.1

http message body length
11 August 2025

Message Body Length in HTTP/1.1


Now, let's discuss this topic briefly. HTTP (Hypertext Transfer Protocol) is one of the essential protocols of the web that transfers data between the server and client. One of the important aspects of this protocol is the message body length. This section is explained in detail in RFC 9112, which specifies how this length should be indicated. As developers, sometimes we need to know how large the data being sent or received is.


In HTTP/1.1, the message body length can be specified in two different ways. The first method is using the Content-Length header, which specifies how large the message body is. By using this header, the server can inform the client about how much data to expect, and the client can also act based on it.


The second method is using the Transfer-Encoding header, where the data is sent in smaller chunks, and the total message size is not precisely defined. This method is usually used for sending large content or when processing is being conducted. It's essential to understand the advantages and disadvantages of each method and choose based on specific needs.


Consequently, we must understand how the choice of one of these two methods can impact performance and the effectiveness of our applications. For example, in situations where data needs to be produced quickly, using chunked transfer might be more efficient. On the other hand, knowing exactly how long the data is can be the best choice when clarity is needed regarding the size of the data.


Example Code


GET /example HTTP/1.1
Host: www.example.com
Content-Length: 123

This is a test message.

Code Explanations



Line One: GET /example HTTP/1.1 indicates an HTTP request of the GET type to the address /example.


Line Two: Host: www.example.com indicates to which host the request is sent.


Line Three: Content-Length: 123 specifies that the body length of the message is 123 bytes.


Line Four: Ignored indicates additional separation between headers and the message body.


Line Five: This is a test message. is the body of the message that will be sent to the server.

FAQ

?

Why should we specify the message body length?

?

What is the difference between Content-Length and Transfer-Encoding?

?

In what situations should I use Transfer-Encoding?