3

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"
   }
]
6
  • Do you have an example of your response object? Commented Jan 13, 2021 at 7:00
  • @Marcin hope your doing well and safe, I have added a json example in the qustion Commented Jan 13, 2021 at 9:06
  • Hi. Im fine. Are you sure this is correct response? It does not have any Items, but you are using response['Items']? Commented Jan 13, 2021 at 9:31
  • I have given you the response['Items'] itself. but it works only with lambda but when I integrate it with API gateway I get the above-mentioned error Commented Jan 13, 2021 at 9:36
  • But in your question you write that "body": response does not work, not "body": response["Items"]? Can you clarify please what works, what does not, and what is the example you provided. Commented Jan 13, 2021 at 9:43

1 Answer 1

6

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']

Sign up to request clarification or add additional context in comments.

1 Comment

could you help me out of this: stackoverflow.com/questions/65913827/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.