Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 820
Limbo Exile Avatar asked Mar 25 '13 16:03

Limbo Exile


People also ask

What is a 411 response code?

What Is a 411 Status Code? The server refuses to accept the request without a defined Content-Length1.

Is content-Length required for get?

The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.

How do I add content-Length to my header?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.


1 Answers

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.

like image 122
Sachin Avatar answered Oct 19 '22 13:10

Sachin