简体   繁体   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. I have an API in the AWS API gateway, connected to an AWS Lambda function in python, using lambda proxy integration. On my local computer, I am testing this API in python.在我的本地计算机上,我正在 python 中测试这个 API。 I want to send a dictionary to the api and have the api return me a function of the elements in the dictionary.我想将字典发送到 api 并让 api 向我返回字典中元素的 function。

The python code I use to test is:我用来测试的 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)

Since I cannot send a list through the header argument of requests.post I thought I'd use json.由于我无法通过 requests.post 的 header 参数发送列表,我想我会使用 json。 When I try this, I get a {"message": "Internal server error"}.当我尝试这个时,我得到一个 {"message": "Internal server error"}。 This is my AWS lambda function (python):这是我的 AWS lambda function(python):

import json

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

Looking at the logs, 'data' is not a key of event.查看日志,“数据”不是事件的关键。 How do I access the list in the AWS Lambda function?如何访问 AWS Lambda function 中的列表? How do I find out what the event dictionary looks like when I test this?测试时如何找出事件字典的样子? Looking forward I want to send a large dict containing dicts and lists.展望未来,我想发送一个包含字典和列表的大字典。 Is this the right way to do it?这是正确的方法吗?

Try尝试

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