簡體   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