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

질문됨 5년 전5577회 조회
1개 답변
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

답변함 5년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠