Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Length-Header not set in Jersey Client request

Tags:

java

jersey

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?

like image 443
Julian Arz Avatar asked Apr 12 '11 18:04

Julian Arz


People also ask

Is content-length header required request?

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.

What is content-length in HTTP request header?

The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.

What is request content-length?

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>


2 Answers

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, "");

like image 173
Pseudopsia Kite Avatar answered Sep 19 '22 22:09

Pseudopsia Kite


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.

like image 42
yves amsellem Avatar answered Sep 17 '22 22:09

yves amsellem