Question

I'm trying to make a request to a Jersey REST service which accepts an InputStream as an entity using the PUT method.

When I do, the Jersey client sets the Content-Length header to 0.

The REST service requires that the Content-Length is set to the actual size of the stream (IE the size of the file), but, whenever I try to set the Content-Length the client throws:

org.apache.http.ProtocolException: Content-Length header already present

To note - the exception is not thrown when I add the Content-Length header, (at that point, this header doesn't exist), it's thrown (I assume) when Jersey / Apache HTTPClient tries to add the 0 header value later.

Stack if it's any help:

Caused by: org.apache.http.client.ClientProtocolException at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827) at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:326) ... 40 more

Caused by: org.apache.http.ProtocolException: Content-Length header already present at org.apache.http.protocol.RequestContent.process(RequestContent.java:96) at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:109) at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:176) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:518) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) ... 42 more

So, any ideas why Jersey / Apache HTTP Client sets the Content-Length header to 0, and, why I'm not able to override the Content-Length header

I'm using:

Jersey 2.4.1 with the ApacheConnector

Thanks in advance

Will

Was it helpful?

Solution

Just to answer this:

This is a problem with the ApacheConnector, which will always return -1 for the size of the content, ensuring this value is never set. (This is hardcoded in the getHttpEntity method)

Therefore, if the content-length header is required, one either has to reimplement the ApacheConnector as described here or use a different connector.

I have reported this on the Jersey jira.

https://java.net/jira/browse/JERSEY-2373

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top