我试过搜索,但我的 googlefu 失败了。
我有一个基本的 python lambda 函数:
def lambda_handler(event, context):
foo = event['bar']
print(foo)
Run Code Online (Sandbox Code Playgroud)
然后我尝试做一个 POST,像这样在 curl 中:
curl -X POST https://correctaddress.amazonaws.com/production/test \
-H 'x-api-key: CORRECTKEY' \
-H 'content-type: application/json' \
-H 'bar: Hello World' \
Run Code Online (Sandbox Code Playgroud)
这KeyError: 'bar'可能失败了,因为我认为我作为 event['bar'] 传递的不是这样传递的。
我试过event['body']['bar']也失败了。
如果我这样做event['querystringparameters']['bar'],如果我使用 GET 会起作用,例如:
curl -X POST https://correctaddress.amazonaws.com/production/test?bar=HelloWorld -H 'x-api-key: CORRECTKEY'
Run Code Online (Sandbox Code Playgroud)
我知道我缺少关于事件字典的一些基本知识,以及它从 POST 中提取的内容,但我似乎找不到正确的文档(不确定它是否会出现在 API 或 Lambda 的文档中)。
我的最终目标是能够使用像这样的请求在 python 中写一些东西:
import requests, json
url = "https://correctaddress.amazonaws.com/production/test"
headers = {'Content-Type': "application/json", 'x-api-key': "CORRECTKEY"}
data …Run Code Online (Sandbox Code Playgroud)