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 .
Thanks to the @gddc 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.