繁体   English   中英

Access json request from python in python AWS lambda function

[英]Access json request from python in python AWS lambda function

I have an API in the AWS API gateway, connected to an AWS Lambda function in python, using lambda proxy integration. 在我的本地计算机上,我正在 python 中测试这个 API。 我想将字典发送到 api 并让 api 向我返回字典中元素的 function。

我用来测试的 python 代码是:

import requests
import json

url = "https://p68yzl6gu6.execute-api.us-west-1.amazonaws.com/test/helloworld"

headers = {'data': [0,1]}

response = requests.post(url, json=headers)

print(response.text)

由于我无法通过 requests.post 的 header 参数发送列表,我想我会使用 json。 当我尝试这个时,我得到一个 {"message": "Internal server error"}。 这是我的 AWS lambda function(python):

import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': event['data']
    }

查看日志,“数据”不是事件的关键。 如何访问 AWS Lambda function 中的列表? 测试时如何找出事件字典的样子? 展望未来,我想发送一个包含字典和列表的大字典。 这是正确的方法吗?

尝试

import json

def lambda_handler(event, context):
    body = json.loads(event['body'])
    return {
        'statusCode': 200,
        'body': json.dumps(body['data'])
    }

暂无
暂无

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

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