Home > Blockchain >  How to get gzip or brotli encoding on Google App Engine
How to get gzip or brotli encoding on Google App Engine

Time:07-24

Does Google App Engine allow compression of the results? For example, I have the following curl request:

$ curl --location --request GET 'https://premiere-stage2.uk.r.appspot.com/' \
> --header 'Accept-Encoding: gzip, deflate, br'

And the response is not compressed. Compare this with something like:

$ curl --location -X GET 'https://google.com' --header 'Accept-Encoding: gzip, deflate, br'
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.

Or, is there something manual I need to set up? I would think the last resort would be to do the compression in the application endpoints themselves, or is that how it needs to be done?

CodePudding user response:

Expanding on John Hanley's suggestion in a comment, there are two parts to this.

  1. You have to set the Accept-Encoding header in the request.
  2. Second, the response itself should have the proper content- or mime-type, such as text/html or whatever it needs to be. Often the web server will ignore compression if the mime-type isn't in a certain list.
  3. Third, to ensure that the headers in both the requests and responses are correct you can use the -v flag in curl.
  4. Finally, it seems the content needs to be over a certain size for the web server to bother compressing it. So, for example, if the content-length is 3, it's not going to be compressed, though I'm not sure exactly what this is.

Putting it all together:

$ curl --location --request GET 'https://premiere-stage2.uk.r.appspot.com/html' 
        --header 'Accept-Encoding: gzip, deflate, br' 
        -v

References:

  • curl
  • GAE (a bit buried, under the Go documentation)

CodePudding user response:

According to documentation

For example, the server may automatically send a gzipped response depending on the value of the Accept-Encoding request header. The application itself does not need to know which content encodings the client can accept.

That gives the impression it should. But the same documentation also says

In addition, the following headers are removed from incoming requests because they relate to the transfer of the HTTP data between the client and server:

  • Accept-Encoding

I tested against our production site and in FireFox, Web Developer Tool shows Accept-Encoding: gzip, deflate, br as a request header and a response header of content-encoding: gzip

However, when I tested against local/dev of our site, Web Developer Tool shows Accept-Encoding: gzip, deflate, br as a request header but the response header didn't include content-encoding: gzip. In addition, printing the headers in Flask/Python, gave a value of None for Accept-Encoding

  • Related