The HTTP 411 Length Required
status code indicates that the server requires a Content-Length
header to be included in the request, but the client did not provide it. This typically occurs when the server needs to know the size of the request body to process it correctly, but the client fails to include the necessary header.
Key Points
- The server requires the
Content-Length
header to process the request. - This status code is commonly used with
POST
orPUT
requests that include a request body. - The client must resend the request with the
Content-Length
header.
Common Causes
- Missing
Content-Length
Header: The client sends a request with a body but does not include theContent-Length
header. - Incorrect Request Format: The client may be using chunked transfer encoding (
Transfer-Encoding: chunked
) instead of specifying theContent-Length
. - Server Configuration: The server is configured to reject requests without a
Content-Length
header.
How to Fix
- Client-Side Fix:
- Add the
Content-Length
header to the request, specifying the size of the request body in bytes. - Ensure the header is correctly calculated and matches the actual body size.
- If using chunked transfer encoding, ensure the server supports it or switch to using
Content-Length
.
- Server-Side Fix:
- If the server can handle chunked transfer encoding, ensure it is properly configured to accept such requests.
- Provide clear error messages to help clients understand the requirement for the
Content-Length
header.
Example
If a client sends a POST
request without a Content-Length
header, the server might respond:
HTTP/1.1 411 Length Required
Content-Type: application/json
{
"error": "Length Required",
"message": "The request must include a 'Content-Length' header."
}
Client-Side Handling
- Always include the
Content-Length
header for requests with a body. - Calculate the size of the request body accurately and include it in the header.
- If using chunked transfer encoding, ensure the server supports it.
Server-Side Handling
- Return a
411 Length Required
status code when theContent-Length
header is missing. - Provide clear instructions in the response body or headers about the requirement.
- Log such errors for monitoring and debugging purposes.