411 Length Required
The server refuses to accept the request without a defined Content-Length header.
Usually happens because:
- ☑ Client sending POST or PUT
- ☑ Server security rules rejecting chunked
🔍 Quick Checklist:
Meaning
The HTTP 411 Length Required client error status code indicates that the server refuses to accept the request without a defined Content-Length header field in the client request.
Root Causes
- Client sending POST or PUT requests carrying body payloads but omitting the Content-Length header field.
- Server security rules rejecting chunked transfer-encoding requests.
| Cause | Frequency |
|---|---|
| Client sending POST or PUT | ⭐⭐⭐⭐⭐ |
| Server security rules rejecting chunked | ⭐⭐⭐⭐ |
Common Mistakes
- Sending empty POST requests without setting Content-Length to 0.
- Assuming chunked transfer-encoding is supported by all legacy proxy gateways.
How to Fix
Framework-Specific Examples
Express middleware rejecting POST/PUT routes lacking Content-Length.
app.use((req, res, next) => {
if (['POST', 'PUT'].includes(req.method) && !req.headers['content-length']) {
return res.status(411).send('Length Required');
}
next();
});Server Configuration Examples
Nginx enforces Content-Length validations automatically.
# Nginx returns 411 immediately if content-length is missing on write methodsPrevention
- Verify client fetch libraries calculate content lengths dynamically.
Frequently Asked Questions (FAQ)
Q: Why does a server require a Content-Length header?
It allows the server to allocate request buffers correctly and protect itself from Denial of Service (DoS) attacks where clients stream bytes indefinitely.
Q: Can I use 'Transfer-Encoding: chunked' instead of Content-Length?
Yes. If chunked encoding is used, Content-Length is not required. However, if the server refuses chunked encoding, it will return 411.
Q: Does 411 apply to GET requests?
No. GET requests have no payload body and thus do not require a Content-Length header.
Q: How do client libraries solve 411 errors?
Most modern client libraries (like Axios or Fetch) calculate the payload byte size and append the Content-Length header automatically.