question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

APIGatewayProxyEvent json_body throws exception if no body

See original GitHub issue

Heya 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:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
heitorlessacommented, Dec 17, 2021

Np- closing this then. We could spend the effort elsewhere like Idempotency serialisation to support dataclasses and Parser (Pydantic) models

1reaction
bb-rrogerscommented, Dec 17, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting json body in aws Lambda via API gateway
Using Lambda Proxy Integration, the body in the event of lambda is a string escaped with backslash, not a JSON. "body": "{\"foo\":\"bar\"}". If...
Read more >
Set up Lambda proxy integrations in API Gateway
The integrated Lambda function verifies all of the input sources before processing the request and responding to the client with meaningful error messages...
Read more >
How to Start Typescript and Node.js AWS Function with ...
In this article, we will talk about starting a project with Serverless and TypeScript. The article requires basic knowledge about AWS ...
Read more >
thirty
verifyJwt is a authentication middleware, which extends the event object by a user object and throws an UnauthorizedError if the client is not...
Read more >
LambdaUtils — sailplane 1.0.0 documentation
Not all Middy middlewares are in this implementation, only common ones that are generally ... @middy/http-header-normalizer@2 @middy/http-json-body-parser@2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found