How to correctly formate the response for an AWS Lambda API?

Viewed 30

I'm trying to understand how to correctly format the JSON that is sent back from my AWS rest API. I am using Api-Gateway to create an API that return the result of a Lambda.

I am used to API responses in the following format:

{
    "name" : "John",
    "age" : 34

}

But, in the AWS example for integrating lambda and api-gateway, they said to use the following response format:

// The output from a Lambda proxy integration must be 
// in the following JSON object. The 'headers' property 
// is for custom response headers in addition to standard 
// ones. The 'body' property  must be a JSON string. For 
// base64-encoded payload, you must also set the 'isBase64Encoded'
// property to 'true'.
let response = {
    statusCode: responseCode,
    headers: {
        "x-custom-header" : "my custom header value"
    },
    body: JSON.stringify(responseBody)
};
return response;

Is there a right way to do this, or can you just return whatever response style you like?

0 Answers
Related