Content-Length-Header not set in jersey client request

I am using a jersey client to access a web service, for example:
response = r.accept(MediaType.TEXT_PLAIN_TYPE).header("content-length", 0).post(String.class);
where r is WebResource

However, the Webservice returns 411 - no Content-Length.

using tcpdump, I found out that I can specify custom headers, i.e. .header("myheader", 0) working fine.

Thus, for some reason, knitwear removes the content length header.

Does anyone have any idea?

+8
java jersey
source share
2 answers

The length of the call content is calculated by the Jersey client, it cannot be set. Paul Sandoz, a well-known project committer, answered a similar question :

Q: I think the "Content-Length" header is not set automatically.

A: Runtime in Jersey [...] determine the length of the content.

If you need more information, please explain what result you expected from the POSTing String for an empty resource.

+3
source share

I really had a requirement to use an empty POST request for a Restful webservice.

If you specify an empty string as the second parameter to the post method, Jersey will create a Content-Length header with a value of 0.

eg. response = r.accept(MediaType.TEXT_PLAIN_TYPE).post(String.class, "");

+6
source share

All Articles