-
Notifications
You must be signed in to change notification settings - Fork 421
Bug: APIGatewayProxyEventV2.decoded_body should not cause KeyError #3706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hey @aahung! Thanks for reporting this bug. You're right, we should use Do you want to send a PR to fix this? We would love it. Thanks |
Hi @aahung could you share how you ended up with the Thank you |
Hello there, In my case it's an actual request from API Gateway HTTP API. Sending a POST without body throws the error (in my case using Postman with Body as none). I'm using Traceback (most recent call last):
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 2002, in _call_route
route(router_middlewares=self._router_middlewares, app=self, route_arguments=route_arguments),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 400, in __call__
return self._middleware_stack(app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 1291, in __call__
return self.current_middleware(app, self.next_middleware)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/event_handler/middlewares/base.py", line 121, in __call__
return self.handler(app, next_middleware)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py", line 117, in handler
received_body=self._get_body(app),
^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py", line 243, in _get_body
return app.current_event.json_body
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lang/lib/python3.12/functools.py", line 995, in __get__
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 153, in json_body
return self._json_deserializer(self.decoded_body)
^^^^^^^^^^^^^^^^^
File "/var/lang/lib/python3.12/functools.py", line 995, in __get__
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 158, in decoded_body
body: str = self["body"]
~~~~^^^^^^^^
File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 32, in __getitem__
return self._data[key]
~~~~~~~~~~^^^^^
KeyError: 'body' |
|
This is now released under 2.36.0 version! |
Expected Behaviour
For event of type
APIGatewayProxyEventV2
,event.decoded_body
orevent.json_body
(which calls the former) could result inKeyError
when there is no body.Current Behaviour
Code snippet
Possible Solution
event.decoded_body
should return empty string orNone
event.json_body
should raiseJSONDecodeError
Steps to Reproduce
As described.
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.11
Packaging format used
PyPi
Debugging logs
No response
The text was updated successfully, but these errors were encountered: