简体   繁体   中英

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. On my local computer, I am testing this API in python. I want to send a dictionary to the api and have the api return me a function of the elements in the dictionary.

The python code I use to test is:

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. When I try this, I get a {"message": "Internal server error"}. This is my 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? 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'])
    }

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.

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