Nodejs lambda function unable to parse event.body getting errors

0

I followed the below post and created a lambda function with lambda proxy api gateway but i'm getting server error when ever i post data for event.body
json.parse(event.body); is throwing errors
Can anyone tell how to parse event.body in node js environment

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-cross-account-lambda-integrations.html

asked 5 years ago5441 views
1 Answer
0

Hi,
Are you still having problems? I followed the directions in the tutorial and was able to successfully invoke the API. I invoked from Postman with:
https://xxxxxxxxg.execute-api.us-east-1.amazonaws.com/prod?name=randy&city=Santa Ana
and in the Headers, I placed Key=day and Value=Monday.
The message response from the lambda function was the expected:

    "message": "Good day, randy of Santa Ana. Happy Monday!",

The only extra step I performed outside of the tutorial was to Deploy the API.
I used the lambda function, as stated in the tutorial using Node.js.8.10 and the parsing worked fine.

'use strict';
console.log('Loading hello world function');
 
exports.handler = async (event) => {
    let name = "you";
    let city = 'World';
    let time = 'day';
    let day = '';
    let responseCode = 200;
    console.log("request: " + JSON.stringify(event));
    
    if (event.queryStringParameters && event.queryStringParameters.name) {
        console.log("Received name: " + event.queryStringParameters.name);
        name = event.queryStringParameters.name;
    }
    
    if (event.queryStringParameters && event.queryStringParameters.city) {
        console.log("Received city: " + event.queryStringParameters.city);
        city = event.queryStringParameters.city;
    }
    
    if (event.headers && event.headers['day']) {
        console.log("Received day: " + event.headers.day);
        day = event.headers.day;
    }
    
    if (event.body) {
        let body = JSON.parse(event.body)
        if (body.time) 
            time = body.time;
    }
 
    let greeting = `Good ${time}, ${name} of ${city}.`;
    if (day) greeting += ` Happy ${day}!`;

    let responseBody = {
        message: greeting,
        input: event
    };
    
    // 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)
    };
    console.log("response: " + JSON.stringify(response))
    return response;
};

In the Lambda function "Designer" screen, if you click on the "Key" icon, the Function Policy shows that the API Gateway from the other account is able to invoke the function in this account:

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "93xxxe-xxxxx-4680-95xx8-aacxxxxxc8b",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "arn:aws:lambda:us-east-1:xxxxxxxxxxxxxxx:function:randyFunction",
      "Condition": {
        "ArnLike": {
          "AWS:SourceArn": "arn:aws:execute-api:us-east-1:xxxxxxxxxxxxxxxxxg/*/GET/"
        }
      }
    }
  ]
}

Hopefully, something from above might help you figure out what steps you may have missed.
-randy

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions