Getting errror : Premature end of chunk coded message body: closing chunk expected with rest-assured 3.3.0

6,136 views
Skip to first unread message

Aswathy Nair

unread,
Jan 27, 2019, 3:56:35 PM1/27/19
to REST assured
Hi,

I am facing below error for a POST request for downloading PDF file. Content-type is "application/pdf"


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)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:225)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:184)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
at org.apache.http.conn.EofSensorInputStream$read.call(Unknown Source)
at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.convertStreamToByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:466)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:197)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:209)
at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:209)
at io.restassured.internal.RestAssuredResponseOptionsImpl.asByteArray(RestAssuredResponseOptionsImpl.java:231)


Tried with 

given()
   .config(RestAssured
                        .config()
                        .connectionConfig(new ConnectionConfig().
                                closeIdleConnectionsAfterEachResponse())
                        .httpClient(HttpClientConfig.httpClientConfig().reuseHttpClientInstance()))...


But i am getting 

java.net.SocketException: Socket is closed
at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1524)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:95)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:158)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:82)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:271)
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:264)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:225)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:184)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
at org.apache.http.conn.EofSensorInputStream$read.call(Unknown Source)
at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.convertStreamToByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:466)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:209)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:207)
at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:209)
at io.restassured.internal.RestAssuredResponseOptionsImpl.asByteArray(RestAssuredResponseOptionsImpl.java:231)


Rest-assured version - 3.3.0

Aswathy Nair

unread,
Jan 29, 2019, 12:20:19 PM1/29/19
to REST assured
Can someone help me on this ?

Regards,

Aswathy

Michael Pinnegar

unread,
Jan 29, 2019, 1:14:18 PM1/29/19
to rest-a...@googlegroups.com
You haven't provided enough relevant details with your question.


The above link provides good guidelines on how to ask questions that will get answers.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aswathy Nair

unread,
Jan 29, 2019, 3:12:04 PM1/29/19
to REST assured
Hi Michael,

Thanks for the guidelines.

Below is the scenario i encountered.


I am trying to download PDF file and writing the content to a local file.  POST request for the same is below . Response from the download request is extracted as a byte[] for further processing.

 byte[] pdf = given()
                .contentType("application/pdf")
                .header("session_token",sessionToken)
                .header("user_id",userId)
                .pathParam("id",id)
                .pathParam("topicId",topicId)
                .when()
                .post("<URI>")
                .then()
                .statusCode(200)
                .extract()
                .asByteArray();

The above request was throwing -- "org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected "

I have modified the request to add "config" for closing idle connections . But that gave me the error for  "java.net.SocketException: Socket is closed"

Below is the modified request:

 byte[] pdf = given()

                .config(RestAssured
                        .config()
                        .connectionConfig(new ConnectionConfig().
                                closeIdleConnectionsAfterEachResponse())
                        .httpClient(HttpClientConfig.httpClientConfig().reuseHttpClientInstance()))
                .contentType("application/pdf")
                .header("session_token",sessionToken)
                .header("user_id",userId)
                .pathParam("id",id)
                .pathParam("topicId",topicId)
                .when()
                .post("<URI>")
                .then()
                .statusCode(200)
                .extract()
                .asByteArray();


If anybody encountered the same , kindly help me how to proceed further.


- Aswathy Nair

Michael Pinnegar

unread,
Jan 29, 2019, 4:03:29 PM1/29/19
to rest-a...@googlegroups.com
I think you might be getting the chunked exception because your original request includes this line

.contentType("application/pdf")

Which is telling the server "My request has a body, and the mime type of that body is a pdf". That's what you should use if you're uploading a PDF. Since your request has no body (that's okay if you're not uploading something on the POST) then the chunked exception is coming from it trying to read the first byte on the server and exploding.

There is an accept header you can set to tell the server the mime type of what kind of responses you want. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept

I would try this.

 byte[] pdf = given()
                .header("session_token",sessionToken)
                .header("user_id",userId)
                .pathParam("id",id)
                .pathParam("topicId",topicId)
                .when()
                .post("<URI>")
                .then()
                .statusCode(200)
                .extract()
                .asByteArray();

Aswathy Nair

unread,
Jan 30, 2019, 3:41:24 AM1/30/19
to REST assured
Hi Michael,

I tried with below request based on your suggestion.

byte[] pdf = given()
                .accept("*/*")
                .header("session_token",sessionToken)
                .header("user_id",userId)
                .pathParam("id",id)
                .pathParam("topicId",topicId)
                .when()
                .post("<URI>")
                .then()
                .statusCode(200)
                .extract()
                .asByteArray();

But it thrown 415 -unsupported Media type.

Later I modified the request like below

  byte[] pdf = given()
                .contentType(ContentType.ANY)
                .header("session_token",sessionToken)
                .header("user_id",userId)
                .pathParam("id",id)
                .pathParam("topicId",topicId)
                .when()
                .post("<URI>")
                .then()
                .statusCode(200)
                .extract()
                .asByteArray();

This one worked fine for me. 

Thanks alot for the prompt support and guidance.

- Aswathy

Michael Pinnegar

unread,
Jan 30, 2019, 4:04:16 AM1/30/19
to rest-a...@googlegroups.com
415 unsupported media type usually comes when the server software doesn't have a method available that matches the request coming in. 

I suspect that adding the content type.any which is */* is making your request work because the controller you're talking to has a @Requestbody annotation on the method you're trying to call in it.

Honestly I have no idea what */* for content type is supposed to mean semantically. I guess its trying to say "I'm sending you any possible format and you have to figure out which one it is by reading the bytes and looking for magic numbers etc"

Glad you got it working though.
Reply all
Reply to author
Forward
0 new messages