Search code examples
google-app-enginegoogle-cloud-platformcompressiongzipbrotli

How to get gzip or brotli encoding on Google App Engine


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?


Solution

  • 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)