简体   繁体   English

AWS Lambda的字典问题-Python

[英]Dictionary problem with aws lambda - Python

I'm developing an AWS lambda function with Python 3.6 and facing an odd scenario. 我正在使用Python 3.6开发一个AWS lambda函数,并且面临一个奇怪的情况。

Locally, calling the function with python-lambda-local everything works fine. 在本地,使用python-lambda-local调用该函数可以正常工作。

In AWS, the snippet below raises an exception: 在AWS中,以下代码段引发异常:

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' AttributeError:'str'对象没有属性'get'

It seems that, locally, the object data is a dict . 在本地看来,对象数据dict But in AWS it is a str . 但是在AWS中这是一个str

Thanks to the @gddc comment I could find the answer. 感谢@gddc评论,我可以找到答案。

The problem is that API Gateway wrap the body value of the event with quotes . 问题在于API Gateway使用引号将事件的主体值包装起来

So I have to parse it first to dict . 所以,我得先分析它与dict。

The correct code: 正确的代码:

def handler(event, context):
    logger.info("###EVENT " + str(event))
    data = event.get('body')
    data = json.loads(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM