nantitv
nantitv

Reputation: 3733

ConnectionClosedException: Premature end of chunk coded message body with apache hhtpclient 4.5.5

I am trying to parse http response from a GET request but it throws following exception.

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:266) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:225) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:184) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135) ~[httpclient-4.5.6.jar!/:4.5.6]
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:na]
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:na]
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:na]
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185) ~[na:na]
    at java.base/java.io.Reader.read(Reader.java:229) ~[na:na]
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:308) ~[httpcore-4.4.10.jar!/:4.4.10]

My code to parse the response is

String parseResponse(HttpResponse resp) {
        try {
            return org.apache.http.util.EntityUtils.toString(resp.getEntity());
        } catch (IOException e) {
                        throw new RuntimeException(e);
        }
    }

I used org.apache.httpcomponents:httpcore:4.5.6

The GET endpoint(spring boot application) which I invoke is as follows

public ResponseEntity<org.springframework.data.domain.
Page<JSONObject>> getList() {
}

I read here https://github.com/jersey/jersey/issues/3629 If I use org.apache.httpcomponents:httpclient:4.5.5 then it should work fine but it did not work for me.

Can someone please give some pointers.

I saw a similar question org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected but it did not help me.

Upvotes: 4

Views: 21386

Answers (1)

nantitv
nantitv

Reputation: 3733

Understood the issue. I was closing CloseableHttpClient and then trying to use the HTTPResponse. Follwing post helped https://stackoverflow.com/a/50136644/1053496

Upvotes: 6

Related Questions