Content-Length
Description
The Content-Length request header is an HTTP header that specifies the size of
the request body in bytes. It informs the server about the exact length of the
data being sent in an HTTP request, which helps in processing and validating the
request properly.
This header is commonly used in POST and PUT requests where the client sends
data to the server. The server can use the Content-Length header to determine
when the request body ends, ensuring complete data transmission.
Syntax
The Content-Length header follows this syntax:
Components:
<length-in-bytes>:
A positive integer representing the number of bytes in the request body.
Examples
Sending JSON Data in a POST Request
A client sends a POST request with a JSON payload:
POST /api/data HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 27
{"name": "John", "age": 30}
The server reads the Content-Length header to expect exactly 27 bytes in the
request body.
Sending Form Data in a PUT Request
A client sends form data in a PUT request:
PUT /update HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
username=johndoe&age=30
The server reads 23 bytes from the request body and processes the form data.