Getting "411 Length Required" after a PUT request from HTTP Client

Limbo Exile picture Limbo Exile · Mar 25, 2013 · Viewed 36.7k times · Source

I am working on a Java program that implements an HTTP Client. I test it sending requests to a server. GET, POST and DELETE requests work ok. For example after a POST request I get an output

Data extracted:
{"status":{"message":"ok"}}

and the database reflects the changes made.

After a PUT request, however I get the following html markup of a webpage indicating an error.

Data extracted:
<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/1.2.6</center>
</body>
</html>

and accordingly no changes in the database.

I found that this can have something to do with the Content-Length header, but I'm not sure. After trying to add this header my program waits for a minute and then throws an exception informing that it couldn't handle the server response.

I can also provide any code or stack trace if needed.

Answer

Sachin picture Sachin · Mar 25, 2013

Yes, the issue related to Content-Length. HTTP Error 411 means

The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

So when you send an empty RequestBody in POST/PUT Method then you also need to send Content-Length:0. So add this header in your request. I don't think this header will cause a problem of you added into Request Object.