0

How do I disable the transfer-encoding chunked in a spring boot web app (2.7.1) or latest version?

I already have these properties.

server:
  compression:
    enabled: true
    min-response-size: 1024
    mime-types: application/json

I also send the Accept-encoding header as gzip.

Even though I see the gzip in the response header, I do not think that it is really compressed (with or w/o gzip, the response size is same with 150 KB). I also do NOT want transfer encoding chunked.

enter image description here


UPDATE: I have another webflux application which works great. I do not see chunked. gzip works just great. This is not anything to do with postman.

  • when I do not send the accept-encoding header

enter image description here

  • for the same request, when I send the accept-encoding header enter image description here
RamPrakash
  • 2,218
  • 3
  • 25
  • 54
  • Why do you think that the response isn't really compressed? The headers suggest otherwise. Depending on the client you're using, it may be unzipping the response body before showing it to you. Also, the transfer encoding has to be chunked as the content length is unknown when gzipping a response. – Andy Wilkinson Aug 01 '22 at 19:57
  • I have used gzip in other apps 150KB is reduced to 10 KB. this app still shows 150KB. also as the response is chunked - it is not even gzipping i believe. – RamPrakash Aug 01 '22 at 20:01
  • What is the request asking for in `accept-encoding` header? See https://stackoverflow.com/a/25046671/2834978 – LMC Aug 01 '22 at 23:01

1 Answers1

0

Your screenshot seems to be from Postman, which shows decompressed size and not the compressed size.

Here's a screenshot from Postman for my Gzip enabled REST service

Order service Postman Response

  1. Content-Encoding is telling client that it content transferred was gzip compressed. It may be incorrect, if you're just setting this header without compressing the response content. However, since you're relying on Spring to do this, it's most likely correct and response is compressed.

  2. Transfer-Encoding just means that response may be sent into multiple HTTP packets. This is done when server does not know the size of response content in advance, before it starts writing to ServletOutputStream. Server can know the size of response content in two ways. First is to buffer serialized response into memory and then get it's byte array length. Second one is to discard serialized content while calculating the size. In the second scenario, server will have to serialize the response twice, once to find size and once to write to ServletOutputStream. As you can see that both of these methods to find response size in advance are not desirable. Server will set this Transfer-Encoding to chunk, even when your response is not Gzipped. That's because JSON serialization in Spring is handled in such a way to allow response to be written to ServletOutputStream as soon as any part of JSON becomes available in the buffer. This way server can serve first byte faster. It will happen even for the smallest response. Having Transfer-Encoding: chunked doesn't mean that your response is not compressed. What it means is that Gzip bytes are written to ServletOutputStream as soon as they become available

  3. Postman shows decompressed size only. You can see actual transferred size and decompressed size in Google Chrome separately.

Chrome shows network transfer size and decompressed size separately

Alternatively you can see both sizes using CURL commands

Using curl to see compressed and uncompressed sizes separately

Here are the commands in text

#Find uncompressed size
curl -so target/test.json localhost:8080/order -w "%{size_download}"

#Find compressed size
curl --compressed -so target/test.json localhost:8080/order -w "%{size_download}"
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • This is not anything to do with postman. Please check the updated question. – RamPrakash Oct 06 '22 at 13:11
  • Have you verified that the first `curl` command above returns full size with `Content-Encoding: gzip` in the response. As I explained `Transfer-Encoding` has nothing to do with response being compressed or not. At this point, unless you post a minimal working example to demonstrate the issue, it'll be very difficult for anyone to answer your question. – 11thdimension Oct 08 '22 at 05:20