I'm using Jersey Client to access a webservice, like this: response =
r.accept(MediaType.TEXT_PLAIN_TYPE).header("content-length", 0).post(String.class);
where r is a WebResource
However, the Webservice returns 411 - Content-Length is missing.
using tcpdump, i found out that i am able to specify custom headers, i.e. .header("myheader", 0)
works fine.
So it seems that jersey is deleting the content-length header for some reasons.
Anyone has any ideas?
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.
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.
The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.
Basically it is the number of bytes of data in the body of the request or response. The body comes after the blank line below the headers. Syntax: Content-Length: <length>
I actually had a requirement to use an empty POST request for a Restful webservice.
If you specify an empty string as the second parameter of post method, Jersey will create the Content-Length header with the value of 0.
e.g.
response = r.accept(MediaType.TEXT_PLAIN_TYPE).post(String.class, "");
The content length of a call is computed by Jersey Client, it cannot be set. Paul Sandoz — a well known commiter on the project — have answered a similar question:
Q: I think that "Content-Length" header is not being set automatically.
A: The Jersey runtime [...] determine the length of content.
If you need further informations, please explain what result did you expect from POSTing a String to a Resource with an empty size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With