JerseyClient post failed with 411 Content-Length required for String Type

2,157 views
Skip to first unread message

Felix Gao

unread,
Feb 17, 2013, 8:18:41 PM2/17/13
to dropwiz...@googlegroups.com
Hi, I am using dropwizard version 0.6.1
Jersey client is created as 
        final JerseyClientBuilder builder = new JerseyClientBuilder()
                .using(configuration.getJerseyClientConfiguration())
                .using(environment);
        final Client jerseyClient = builder.build();
with the following properties
#http Client property for jersey
httpClient:
  # timeout after 3s while connecting, reading, or writing
  timeout: 3s
  # keep connections open for 10 minutes
  timeToLive: 10m
  # don't track cookies
  cookiesEnabled: false
#  gzipEnabled: true # allow for gzipped request and response entities
# If true, the client will encode request entities with gzip
# content encoding. (Requires gzipEnabled to be true).
#  gzipEnabledForRequests: true
  minThreads: 1
  maxThreads: 128 # thread pool for JerseyClient's async requests


this is code snipped that I ran to get the error

 @Override
    public void run() {
        for(Location l : this.locations){  //Location is just a simple pojo 
            String cr = null;
            try {
                final String s =  mapper.writeValueAsString(l);
                cr = this.resource
                        .type(MediaType.APPLICATION_JSON_TYPE)
                        .accept(MediaType.APPLICATION_JSON_TYPE)
                        .post(String.class, s);
            } catch (JsonProcessingException e) {
                logger.error("JSON process error for "+l, e);
            }
        }
    }

this is what I got back from the server
2 * Client out-bound request
2 > Content-Type: application/json
2 > Accept: application/json
� �ێ�0 E ��� "_0 ޘD��� �L3����68!0 '-�����y��^��wP��C�4� rg D� � ��� P* B�9�� �!� D� S
�k�W7� [ʶ�6
���GF���3V�� �5�j ւ >}�A� �rf��1Dā�A� �!�B����? lP"�
Exception in thread "pool-4-thread-1" com.sun.jersey.api.client.UniformInterfaceException: Client response status: 411
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
at com.katami.concurrent.thread.LocationRestInsertion.run(LocationRestInsertion.java:58)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
2 * Client in-bound response
2 < 411
2 < Server: nginx/0.7.65
2 < Date: Mon, 18 Feb 2013 01:14:30 GMT
2 < Content-Type: text/html
2 < Content-Length: 181
2 < Connection: close
2 < 
<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>

I am very confused on why jersey is not doing the right thing by adding the Content-Length header and why is the content in binary when I explicitly set it to String? 

Felix Gao

unread,
Feb 18, 2013, 3:35:43 PM2/18/13
to dropwiz...@googlegroups.com
After some wiresharking, it appears to be a problem with nginx version that I have which can't really upgrade.  Is there a way to force the Jersey client to be in http/1.0 or disable the chunked transfer encoding? 

Vladimir Derkach

unread,
Mar 6, 2013, 7:03:35 AM3/6/13
to dropwiz...@googlegroups.com
try to use MultivaluedMap for passing parameters

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("code", code);
.post(String.class, params);

instead of 

.post(String.class, s);

sonaliv...@gmail.com

unread,
Dec 7, 2018, 1:15:59 AM12/7/18
to dropwizard-user
Hi, 
I am also getting the same error that <API URL> returned a response status of 411 Length Required.

I want to read header passed in POSTMAN in code . Please help me how to fetch Header send in POSTMAN using java REST API.
Reply all
Reply to author
Forward
0 new messages