How to test if event
contains body
json element? I get the following error [ERROR] KeyError: 'body'
. I want to ensure that even curl can call lambda function as well as other lambda functions can call this lambda. But when the request is not through curl, then there is no body element hence I'm trying to create a if condition to set variables.
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"
})
}