Skip to content

Commit

Permalink
fix(event_handler): fix compress handling (#3420)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfonseca authored Nov 28, 2023
1 parent dc9a237 commit ac577b8
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,18 +789,19 @@ def _route(self, event: ResponseEventT, cors: Optional[CORSConfig]):

def build(self, event: ResponseEventT, cors: Optional[CORSConfig] = None) -> Dict[str, Any]:
"""Build the full response dict to be returned by the lambda"""

# We only apply the serializer when the content type is JSON and the
# body is not a str, to avoid double encoding
if self.response.is_json() and not isinstance(self.response.body, str):
self.response.body = self.serializer(self.response.body)

self._route(event, cors)

if isinstance(self.response.body, bytes):
logger.debug("Encoding bytes response with base64")
self.response.base64_encoded = True
self.response.body = base64.b64encode(self.response.body).decode()

# We only apply the serializer when the content type is JSON and the
# body is not a str, to avoid double encoding
elif self.response.is_json() and not isinstance(self.response.body, str):
self.response.body = self.serializer(self.response.body)

return {
"statusCode": self.response.status_code,
"body": self.response.body,
Expand Down

0 comments on commit ac577b8

Please sign in to comment.