powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: compressed routes do not send Vary: Accept-Encoding

Open anishmehta24 opened this issue 1 month ago • 3 comments

Expected Behaviour

When compression is enabled for a route (@app.get(..., compress=True) or Response(compress=True)), the response body depends on the request's Accept-Encoding, so the response should carry Vary: Accept-Encoding. That tells API Gateway caching, CloudFront and other shared caches to keep gzipped and plain responses apart. This is the same reasoning as the Vary: Origin that CORS responses got in #8638.

Current Behaviour

ResponseBuilder gzips the body and sets Content-Encoding: gzip, but never adds Accept-Encoding to Vary, neither on the compressed response nor on the plain one served to a client without gzip. A cache keyed only on the URL can store the gzipped body and serve it to a client that did not ask for gzip, or keep serving the plain body to everyone.

Code snippet

from aws_lambda_powertools.event_handler import ApiGatewayResolver, Response, content_types

app = ApiGatewayResolver()

@app.get("/report", compress=True)
def report():
    return Response(200, content_types.TEXT_PLAIN, "Foo")

result = app({"path": "/report", "httpMethod": "GET", "headers": {"Accept-Encoding": "gzip"}}, None)
result["multiValueHeaders"]
# {'Content-Type': ['text/plain'], 'Content-Encoding': ['gzip']}   no Vary

Steps to Reproduce

  1. Define a route with compress=True.
  2. Call it with and without Accept-Encoding: gzip.
  3. Neither response has a Vary header.

Possible Solution

When compression is enabled for the route or the response, add Accept-Encoding to Vary, keeping any Vary value the route already set (as the CORS code does for Origin). I can open a PR.

Powertools for AWS Lambda (Python) version

latest (develop, 3.38.0)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

anishmehta24 avatar Oct 30 '26 16:10 anishmehta24

Thanks @anishmehta24, I agree, same reasoning we used for Vary: Origin in #8638. Reviewing #8711. Goes into 3.40.0.

leandrodamascena avatar Oct 30 '26 17:10 leandrodamascena

[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

This is now released under 3.40.0 version!

github-actions[bot] avatar Nov 10 '26 11:11 github-actions[bot]