【发布时间】:2021-12-16 17:58:01
【问题描述】:
如何测试event 是否包含body json 元素?我收到以下错误[ERROR] KeyError: 'body'。我想确保即使 curl 可以调用 lambda 函数以及其他 lambda 函数可以调用这个 lambda。但是当请求不是通过 curl 时,没有 body 元素,因此我试图创建一个 if 条件来设置变量。
from modules.ZabbixSender import ZabbixSender
import json
def lambda_handler(event, context):
print(event)
if event["body"]: // KEY ERROR
requestBody = json.loads(event["body"])
else:
requestBody = json.loads(event)
print(requestBody)
Host = requestBody['Host']
Key = requestBody['Key']
Value = requestBody['Value']
sender = ZabbixSender("10.10.10.10", 10051)
sender.add(Host, Key, Value)
sender.send()
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Host": Host,
"Key" : Key,
"Value" : Value,
"Status": "Successfully updated Zabix Server"
})
}
【问题讨论】:
标签: python json python-3.x aws-lambda