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

3

14

I am trying out RestAssured & wrote the following statements -

String URL = "http://XXXXXXXX";
Response result = given().
            header("Authorization","Basic xxxx").
            contentType("application/json").
            when().
            get(url);
JsonPath jp = new JsonPath(result.asString());

On the last statement, I am receiving the following exception :

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

The headers returned in my response are :

Content-Type → application/json; qs=1 Date → Tue, 10 Nov 2015 02:58:47 GMT Transfer-Encoding → chunked

Could anyone guide me on resolving this exception & point me out if I am missing anything or any in-correct implementation.

Necrology answered 10/11, 2015 at 15:28 Comment(2)
Which version of rest-assured do you use?Torrez
I was using an older version of RestAssured & now tried with 2.4.1 version & it seems to work. Thanks @fiddler for pointing it out.Necrology
A
7

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();
}

Anthracosilicosis answered 31/7, 2019 at 14:22 Comment(0)
G
0

This error also happens if you forward a response from another http client without wrapping it into a new ResponseEntity.

Let me clarify with two examples coming from an api controller class:

This will throw ConnectionClosedException:

fun getFoo(): ResponseEntity<List<Foo>> {
    return httpClient.getFoo() //this will throw exception
}

Better do this:

fun getFoo(): ResponseEntity<List<Foo>> {
    val response = httpClient.getFoo()
    return ResponseEntity.status(response.statusCode).body(response.body)
}
Granivorous answered 31/5 at 11:44 Comment(0)
N
-1

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

given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..
Nolde answered 10/11, 2015 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.