0

I create pdf document dynamically and want to serve them in my handler. I set the content-type to application/pdf and it works fine. I run my server through nginx proxy.

My problem is that some requests generate a lot of other requests for the same doc. I looked at the headers and seen that it want a Chunked transfer encoding.

My solution was to set the content-length and it seems to works fine.

I wonder if it's enough and why I never had to do it with simple html page.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • This is probably related to your nginx config. The client shouldn't care if it's chunked or not. – JimB Aug 16 '17 at 15:23

1 Answers1

2

A comment in the source code says:

If the handler didn't declare a Content-Length up front, we either go into chunking mode or, if the handler finishes running before the chunking buffer size, we compute a Content-Length and send that in the header instead.

If you want to avoid chunking, then set the content length. Setting the content length for a large response does reduce the amount of data transferred and can reduce copying within the HTTP server.

As a rule of thumb, set the content length if the length is known in advance of producing the response body.

Your simple HTML pages may be smaller than the chunking buffer size. If so, they were not chunked.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242