How to return gzipped content with AWS API Gateway

泄露秘密 提交于 2019-11-28 09:52:43
Pablo Arias Mora

Since Dec 19, 2017. AWS API Gateway has encoding support.

After the API is created, just go to Settings and select Content Encoding Enable.

Also here is the AWS official release post.

Unfortunately, API Gateway does not currently support HTTP compression. However, it is in consideration for future development.

For now, you will need to return uncompressed content from your endpoint (i.e. omit Accept-Encoding header) in order to proxy it through API Gateway.

If it works for your use case you could alternatively base64 encode the compressed content, proxy it through API Gateway, and decode it on the client.

Thanks, Ryan

Only a workaround, but if you set Accept-Encoding: identity you should receive the result correctly (contrary to the linked discussion I have found it works for POST and GET).

I made it work by adding Accept-Encoding to AWS API Gateway Integration Request

Step 1: Go to AWS API Gateway console, click on Integration Request

Step 2: Add Accept-Encoding to HTTP Headers section, value 'identity' (need single quotes)

Step 3: Click Actions -> Deploy API

If you're using AWS CloudFormation, you can add yaml Integration: RequestParameters: integration.request.header.Accept-Encoding: "'identity'

Although this question is a bit old, I'd like to add an answer since this question is top-most viewed one. In fact, there are 2 scenarios related to returning the compressed content.

The first scenario is when you want API Gateway to compress the content. As the accepted answer suggests, you can enable the content encoding on your API then deploy it.

The second scenario is your integration endpoint already compressed the result and you just want to bypass it via API Gateway. The traditional workaround was configuring it as a binary media type. However, this might be problematic since it will begin to treat all response with the media type as a binary. Additionally, if you need to deal with multiple media types, your only choice would be setting it as '*'. If you're using non-proxy integration, you will lose a chance to transform the result.

To address the second issue, now API Gateway implicitly assumes a response result as binary when a proxy integration is used AND the content is encoded (the response has Content-Encoding with the value other than 'identity'). You don't need to configure binary media types any more when these conditions are met. However, if you're returning the actual binary media (e.g. image, video), you still need to configure them as binary media type(s).

You might wonder what happens when you have both scenarios. The short answer is that API Gateway will not compress again when the response already has Content-Encoding header.

Hopefully this helps.

Amee Hirani

To allow GZipped content from HTTP proxy endpoint you can add */* in "Binary media types" found at "Binary Support" section of your API.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!