Closed
Description
Expected Behaviour
Once compress flag is set to true on either Response object or in the router annotation the lambda should compress the response and return it as an encoded base 64 string.
Current Behaviour
It throws an error saying: a bytes-like object is required, not 'list'
Code snippet
from typing import List
import requests
from pydantic import BaseModel, Field
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver(debug=True)
class Todo(BaseModel):
userId: int
id_: int = Field(alias="id")
title: str
completed: bool
@app.get("/todos", compress=True)
def get_todos() -> List[Todo]:
todo = requests.get("https://jsonplaceholder.typicode.com/todos")
todo.raise_for_status()
return todo.json()
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)
Possible Solution
No response
Steps to Reproduce
- Use any API Request (either REST or HTML) you have that returns a json and add
compress=True
on the router. (if you want to return a Response as an object, you can do that too; add the compress flag on the response object as well) - Create a test event to call that route and include
"Accept-Encoding": "gzip, deflate, br"
in the request header - Call the function with that event
Powertools for AWS Lambda (Python) version
2.28.0
AWS Lambda function runtime
3.11
Packaging format used
Lambda Layers
Debugging logs
Function Logs
ogger to suppress child logger records to bubble up
2023-11-27 21:57:35,739 aws_lambda_powertools.logging.logger [DEBUG] Adding filter in root logger to suppress child logger records to bubble up
2023-11-27 21:57:35,739 aws_lambda_powertools.logging.logger [DEBUG] Marking logger service_undefined as preconfigured
[DEBUG] 2023-11-27T21:57:35.739Z Marking logger service_undefined as preconfigured
2023-11-27 21:57:35,741 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Adding route using rule /todos and methods: GET
[DEBUG] 2023-11-27T21:57:35.741Z Adding route using rule /todos and methods: GET
START RequestId: 4091d393-77ec-4e22-841d-f42423544ed8 Version: $LATEST
{"version":"1.0","resource":"/todos","path":"/todos","httpMethod":"GET","headers":{"Origin":"https://aws.amazon.com","Accept-Encoding":"gzip, deflate, br"},"multiValueHeaders":{},"queryStringParameters":{},"multiValueQueryStringParameters":{},"requestContext":{"accountId":"123456789012","apiId":"id","authorizer":{"claims":null,"scopes":null},"domainName":"id.execute-api.us-east-1.amazonaws.com","domainPrefix":"id","extendedRequestId":"request-id","httpMethod":"GET","path":"/todos","protocol":"HTTP/1.1","requestId":"id=","requestTime":"04/Mar/2020:19:15:17 +0000","requestTimeEpoch":1583349317135,"resourceId":null,"resourcePath":"/todos/1","stage":"$default"},"pathParameters":null,"stageVariables":null,"body":"","isBase64Encoded":false}
2023-11-27 21:57:35,744 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Converting event to API Gateway REST API contract
[DEBUG] 2023-11-27T21:57:35.744Z 4091d393-77ec-4e22-841d-f42423544ed8 Converting event to API Gateway REST API contract
2023-11-27 21:57:35,744 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Found a registered route. Calling function
[DEBUG] 2023-11-27T21:57:35.744Z 4091d393-77ec-4e22-841d-f42423544ed8 Found a registered route. Calling function
2023-11-27 21:57:35,744 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Building middleware stack: []
[DEBUG] 2023-11-27T21:57:35.744Z 4091d393-77ec-4e22-841d-f42423544ed8 Building middleware stack: []
Processing Route:::get_todos (/todos)
Middleware Stack:
=================
_registered_api_adapter
=================
2023-11-27 21:57:35,744 aws_lambda_powertools.event_handler.api_gateway [DEBUG] MiddlewareFrame: [_registered_api_adapter] next call chain is _registered_api_adapter -> get_todos
[DEBUG] 2023-11-27T21:57:35.744Z 4091d393-77ec-4e22-841d-f42423544ed8 MiddlewareFrame: [_registered_api_adapter] next call chain is _registered_api_adapter -> get_todos
2023-11-27 21:57:35,745 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Calling API Route Handler: {}
[DEBUG] 2023-11-27T21:57:35.745Z 4091d393-77ec-4e22-841d-f42423544ed8 Calling API Route Handler: {}
[DEBUG] 2023-11-27T21:57:36.107Z 4091d393-77ec-4e22-841d-f42423544ed8 Simple response detected, serializing return before constructing final response
2023-11-27 21:57:36,107 aws_lambda_powertools.event_handler.api_gateway [DEBUG] Simple response detected, serializing return before constructing final response
[ERROR] TypeError: a bytes-like object is required, not 'list'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 32, in lambda_handler
return app.resolve(event, context)
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 1736, in resolve
response = self._resolve().build(self.current_event, self._cors)
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 792, in build
self._route(event, cors)
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 788, in _route
self._compress()
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 773, in _compress
self.response.body = gzip.compress(self.response.body) + gzip.flush()END RequestId: 4091d393-77ec-4e22-841d-f42423544ed8
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Shipped