APIGatewayProxyEvent json_body throws exception if no body
See original GitHub issueHeya All,
Not sure if this would be seen as an issue or not, but I thought I would log it anyways. When using the APIGatewayProxyEvent.json_body function I have found that if there is no body, it will throw a TypeError:
[ERROR] TypeError: the JSON object must be str, bytes or bytearray, not NoneType
I feel on the fence whether it’s better to validate that there is a body before attempting to json_body but I guess with the return type hint being Any I was maybe expecting a None or empty string.
Expected Behavior
I expect the json_body should return an empty string if the object is not present in the payload
Current Behavior
Function throws a TypeError because json.loads cannot handle None objects.
Steps to Reproduce (for bugs)
app = ApiGatewayResolver()
@app.get("/test")
def get_test():
app.current_event.json_body
def handler(event: dict, context: dict) -> dict:
return app.resolve(event=event, context=context)
Including a REST request with an None body.
Again, not sure if this should be considered a bug or not, looking for guidance here, is a minor issue.
Environment
- Powertools version used: 1.22.0
- Packaging format (Layers, PyPi): Layers
- AWS Lambda function runtime: Python 3.9
- Debugging logs
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Np- closing this then. We could spend the effort elsewhere like Idempotency serialisation to support dataclasses and Parser (Pydantic) models
Thanks both. The intent was to stir up discussion here but through further reflection I think maybe it’s better to keep it as is, I can see positives for and against but will let you guys ultimately decide. I just wanted to log this to figure out the reasoning here.