12

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.

Limbo Exile
  • 1,321
  • 2
  • 21
  • 41

1 Answers1

18

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • 1
    I tried this also, what I get in this case is `Data extracted: {"status":{"message":"Content type 'application/octet-stream' not supported"}}` and still no changes in database – Limbo Exile Mar 25 '13 at 16:21
  • This is another issue which is related to Content-type. It may be possible that your server is not reporting the proper mime types or not supported this content-type – Sachin Mar 25 '13 at 16:23
  • I contacted the server and they informed me that they tested the same PUT request and it works with their implementation of HTTP Client. The thing is I use a dedicated Java ME platform and I assume there is a bug in my version of HTTP Client. What I cannot understand is how does it work with POST then? – Limbo Exile Mar 25 '13 at 16:27
  • Try to get `Content-Type` Header value from whoever successfully put the request. I think it just a simple mistake of Header values in your request – Sachin Mar 25 '13 at 16:36