Skip to content

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

Open

Description

@anishmehta24

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

Activity

  1. leandrodamascena commented on Oct 30, 2026

    @leandrodamascena
    Contributor

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

  2. added this to the 3.40.0 milestone on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions