简体   繁体   English

KeyError: 'Host' - 如何从 AWS Lambda 中的 URL 访问 JSON 正文

[英]KeyError: 'Host' - How to access JSON body from URL in AWS Lambda

How can I retrieve JSON data sent to AWS lambda function?如何检索发送到 AWS lambda 函数的 JSON 数据? I need to pass the data sent to AWS lambda to one of the function in the code.我需要将发送到 AWS lambda 的数据传递给代码中的函数之一。 But I'm getting KEY Error.但我收到 KEY 错误。 Any suggestion on how to resolve it?关于如何解决它的任何建议?

In the cloud watch I see that the data is shown as:在云表中,我看到数据显示为:

"body": "{ \\"Host\\": \\"Zabbix server\\", \\"Key\\": \\"lambda.test\\", \\"Value\\": \\"5.96\\" }",

I'm getting the following error:我收到以下错误:

[ERROR] KeyError: 'Host'
Traceback (most recent call last):
  File "/var/task/AlertMetricSender.py", line 14, in lambda_handler
    Host = event['Host']

I'm sending the request as follows:我发送请求如下:

curl -X POST 'https://myapi.execute-api.ap-south-1.amazonaws.com/default/zabbixPy'  -H 'Content-Type: application/json' -d '{ "Host": "Zabbix server", "Key": "lambda.test", "Value": "5.98" }'

AlertMetricSender.py:

from modules.ZabbixSender import ZabbixSender
import json

def lambda_handler(event, context):

  Host = event['Host']
  Key = event['Key'] 
  Value = event['Value']

  sender = ZabbixSender("10.10.10.10", 10051)
  sender.add(Host, Key, Value)
  sender.send()

You are missing the 'body'.你缺少“身体”。 Try the below试试下面的

Host = event['body']['Host']

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

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