简体   繁体   中英

Content-Length-Header not set in Jersey Client request

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, ie .header("myheader", 0) works fine.

So it seems that jersey is deleting the content-length header for some reasons.

Anyone has any ideas?

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.

eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM