org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected apache apache

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected


I had a similar issue that wasn't related to , but this was the first result Google found so I'm posting my answer here, in case others face the same issue.

For me, the problem was (as ConnectionClosedException clearly states) closing the connection before reading the response. Something along the lines of:

CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try {    doSomthing();} finally {    response.close();}HttpEntity entity = response.getEntity();InputStream instream = entity.getContent(); // Response already closed. This won't work!

The Fix is obvious. Arrange the code so that the response isn't used after closing it:

CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try {    doSomthing();    HttpEntity entity = response.getEntity();    InputStream instream = entity.getContent(); // OK} finally {    response.close();}


Perhaps you can try fiddling around with the connection config? For example:

given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..