2

I'm developing an AWS lambda function with Python 3.6 and facing an odd scenario.

Locally, calling the function with python-lambda-local everything works fine.

In AWS, the snippet below raises an exception:

def handler(event, context):
    data = event['body']
    logger.info("###DATAAAAA BODY " + str(data))
    origem = data.get('origem','')

Error:

AttributeError: 'str' object has no attribute 'get'

It seems that, locally, the object data is a dict. But in AWS it is a str.

1
  • 1
    You've assigned data to be event['body']. If that's a string, then you don't have a .get() method. Perhaps log the entire contents of the event and double-check what you're receiving? Commented Sep 25, 2018 at 19:56

1 Answer 1

4

Thanks to the @g.d.d.c comment I could find the answer.

The problem is that API Gateway wrap the body value of the event with quotes.

So I have to parse it first to dict.

The correct code:

def handler(event, context):
    logger.info("###EVENT " + str(event))
    data = event.get('body')
    data = json.loads(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.