AWS Lambda-API gateway "message": "Internal server error" (502 Bad Gateway)
This error occurs due to the behaviour of the event object (python dictionary). When you test lambda function in lambda console JSON body will directly passed to the event object. But when you try it through API gateway, not only event object is the request payload but also body attribute is set as a string.
For example your JSON body will be like this in event object
body: "{\n \"val1\": \"3\",\n \"val2\": \"5\"\n}"
To resolve this error try json.loads() method to convert body string to json.
import json
def lambda_handler(event, context):
# TODO implement
try:
event = json.loads(event['body'])
val1 = int(event['val1'])
val2 = int(event['val2'])
val3 = val1 + val2
except:
val3 = 'request error'
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(val3)
}