Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Client doesn't set Content-Length

I am using dropwizard for writing a webapp and also using Jersey Client as mentioned at http://dropwizard.codahale.com/manual/client/#man-client-jersey

But it seems that whenever i try to do a post using the jersey client the remote webservice complains that Content-Length header is missing and fails.

public JobResponse createJob(JobRequest job) {
        return jerseyClient.resource(URI.create(JOBS_URL))
                .type(MediaType.APPLICATION_JSON_TYPE)
                .header("Api-Key", job.getApiKey())
                .post(JobResponse.class, job);
    }

I have confirmed that the request does not contain the header and despite my best efforts I haven't been able to figure out why this is happening. Does anyone know if there is something that I am missing?

PS: The service that i am trying to hit is https://app.zencoder.com/docs/api/jobs/create

like image 363
Ankur Chauhan Avatar asked Sep 27 '12 04:09

Ankur Chauhan


People also ask

Is content-length set automatically?

When making a POST request, HTTP clients typically automatically add a Content-Length header based on the size of the data supplied and a Content-Type header based on the type of data being transferred.

Is content-length mandatory?

Description. The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.

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.


2 Answers

This is known "issue" and actually intended behavior.

Problem here is that entity is processed AFTER headers are written out to "the wire", thus Content-Length header value is not know when headers are serialized. If you need to have it, you have several options (with various complexity):

  1. serialize entity by yourself; if you provide entity as string (or byte[]), Content-Length should be set.

  2. create your own MessageBodyWriter, which would compute size of entity in getSize() method call.

there might be some other way how to do it, but I can't think of another right now.. hope it helps.

like image 190
Pavel Bucek Avatar answered Sep 28 '22 03:09

Pavel Bucek


I was facing the same problem and the answer from Pavel didn't work out for me (I was using a FormMutiPart object).

I was using ApacheHttpClient4 instead of the regular com.sun.jersey.api.client.Client. Changing back to the Jersey Client, the Content-Lenght is calculated (at least in the case of FormMultiPart entity).

like image 38
Didac Montero Avatar answered Sep 28 '22 02:09

Didac Montero