Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 592 forks source link

Parsing error occurs when POST response is empty gzip content type #349

Closed sthamman closed 4 years ago

sthamman commented 4 years ago

Describe the bug Parsing error occurs when POST response is empty gzip content type

To Reproduce

Server

response.setContentType("application/json; charset=utf-8");
response.setHeader("Content-Encoding", "gzip");
response.setStatus(HttpServletResponse.SC_ACCEPTED);
response.getWriter().write(new char[0]); // optional

Client

Unirest.post("http://localhost:8080")
  .asObject(Map.class)
  .getParsingError()
  .get();

Stack Trace

kong.unirest.UnirestParsingException: kong.unirest.UnirestException: java.io.EOFException
    at kong.unirest.BaseResponse.setParsingException(BaseResponse.java:93)
    at kong.unirest.BasicResponse.<init>(BasicResponse.java:48)
    at kong.unirest.apache.BaseApacheClient.transformBody(BaseApacheClient.java:56)
    at kong.unirest.apache.ApacheClient.request(ApacheClient.java:127)
    at kong.unirest.BaseRequest.asObject(BaseRequest.java:249)
    at io.cloudio.scale.kafka.rest.FlowrestTest.main(FlowrestTest.java:33)
Caused by: kong.unirest.UnirestException: java.io.EOFException
    at kong.unirest.apache.ApacheResponse.getContentAsBytes(ApacheResponse.java:89)
    at kong.unirest.apache.ApacheResponse.getContentAsString(ApacheResponse.java:107)
    at kong.unirest.apache.ApacheResponse.getContentAsString(ApacheResponse.java:97)
    at kong.unirest.ObjectResponse.readBody(ObjectResponse.java:57)
    at kong.unirest.ObjectResponse.<init>(ObjectResponse.java:40)
    at kong.unirest.BaseRequest.lambda$asObject$3(BaseRequest.java:249)
    at kong.unirest.apache.BaseApacheClient.transformBody(BaseApacheClient.java:53)
    ... 3 more
Caused by: java.io.EOFException
    at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:268)
    at java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:258)
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
    at kong.unirest.apache.ApacheResponse.getContentAsBytes(ApacheResponse.java:85)
    ... 9 more

Expected behavior Parsing error should not occur

Environmental Data: