I have a lambda function, which is returning me a value from dynamdb table. The value is in nested jason format.
When I test the lambda in it's own console it is working fine. BUT When I integrate it with API gateway it is giving me an internal server error with this message.
Execution failed due to configuration error: Malformed Lambda proxy response
here is my response body
return {
"isBase64Encoded": "true",
"statusCode": 200,
"headers": { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" },
"body": response
}
Does anyone know this issue.
I tried with
return {
"isBase64Encoded": "true",
"statusCode": 200,
"headers": { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" },
"body": json.dumps(response['Items'])
}
it seems to be working.
What is the issue causing this? because if I have a normal JSON.
"body": response
works properly. The issue only comes when there is nested JSON
Here is the JSON object:
[
{
"time_stamp":"2021-01-13 06:02:42",
"broker_id":"broker1",
"load_id":"ab6fd05f-3f54-44dc-ae6d-28e924fe1ae2",
"messages":[
{
"date":"22:32",
"messanger_id":"[email protected]",
"message":"Hello",
"user":"carrier"
},
{
"date":"22:35",
"messanger_id":"[email protected]",
"message":"Hi",
"user":"broker"
}
],
"carrier_id":"carrier1"
},
{
"time_stamp":"2021-01-13 06:03:32",
"broker_id":"broker1",
"load_id":"ab6fd05f-3f54-44dc-ae6d-28e924fe1ae2",
"messages":[
{
"date":"22:32",
"messanger_id":"[email protected]",
"message":"Hello",
"user":"carrier"
},
{
"date":"22:35",
"messanger_id":"[email protected]",
"message":"Hi",
"user":"broker"
}
],
"carrier_id":"carrier2"
}
]
It works with json.dumps because you are getting JSON string, which is required for proxy lambda integration. From docs:
With the Lambda proxy integration, the Lambda function must return output of the following format:
{
statusCode: "...", // a valid HTTP status code
headers: {
custom-header: "..." // any API-specific custom header
},
body: "...", // a JSON string.
isBase64Encoded: true|false // for binary support
}
As you can see JSON string must be used in body. You can't return plain json object as you do when you write "body": response['Items']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With