The HTTP 411 status code stands for "Length Required". It is a client error response indicating that the server refuses to accept the request because the Content-Length header is missing and required.

When is HTTP 411 Used?

  • When the server needs to know the size of the request body before processing it.
  • Often required in POST, PUT, or PATCH requests where data is sent in the request body.
  • Frequently used by proxies or servers with resource limitations to allocate resources properly.

How Does It Work?

The server expects a Content-Length header to know how much data it should expect in the request body. If the client fails to include this header, the server responds with a 411 Length Required status.

Example Scenarios

  • Uploading files: The server needs to know the file size to allocate memory.
  • API requests: An API may require Content-Length to validate and process data correctly.
  • Proxies: Some proxies require the Content-Length header to manage traffic efficiently.

Example

Client Request (Missing Content-Length Header)

    
        POST /upload HTTP/1.1
        Host: example.com
        Content-Type: application/json

        { "file": "large-data" }
    

(The Content-Length header is missing here.)

Server Response (411 Length Required)

    
        HTTP/1.1 411 Length Required
        Content-Type: application/json

        { "error": "Content-Length header is required for this request." }
    

Fixing HTTP 411 Errors

To resolve a 411 error, the client needs to add the Content-Length header specifying the size of the request body in bytes.

Corrected Request (With Content-Length Header)

    
        POST /upload HTTP/1.1
        Host: example.com
        Content-Type: application/json
        Content-Length: 25

        { "file": "large-data" }
    

Best Practices for Handling HTTP 411

  • Always Include Content-Length: When sending data in the request body, calculate and include the Content-Length.
  • Use Transfer-Encoding: chunked if needed: If the body size is unknown at the start, use the chunked transfer encoding.
  • Server Configuration: Configure servers to handle requests properly and clearly document when Content-Length is required.

Summary

The HTTP 411 Length Required status code indicates that the server requires the Content-Length header to process the request. It ensures the server knows the size of the request body before proceeding. Adding the Content-Length header or using chunked encoding can resolve this error.