Nodejs API call returning undefined to lambda function

Viewed 13426

This is the aws lambda function which will invoke an api:

'use strict';

var request = require("request")

exports.handler = function (event, context,callback) {



let url = "https://3sawt0jvzf.execute-api.us-east-1.amazonaws.com/prod/test"

request({
    url: url,
    method: "POST",
    json: event,

}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        callback(null, { "isBase64Encoded": true|false,
                          "statusCode": "200",
                          "headers": { "headerName": "headerValue"},
                          "body": body});
    }
    else {

        console.log("error: " + error)
        console.log("response.statusCode: " + response.statusCode)
        console.log("response.statusText: " + response.statusText)
    }
})
};

This is the api written as an aws lambda function:

'use strict';


exports.handler = function(event, context, callback) {
console.log(event.name);
callback(null, { "isBase64Encoded": true|false,
                 "statusCode": "200",
                 "headers": { "headerName": "headerValue"},
                 "body": `Hello World ${event.name}`});  // SUCCESS with message
};

When I try to call the api from the lambda function it just returns "Hello World undefined". It is not appending the name at the end and returning the correct response.

3 Answers

Assumptions:

  • You're using Lambda-Proxy Integration.
  • You want to pass the the exact same payload that the first Lambda received to the second Lambda.*

You're misunderstanding what event is. This is NOT the JSON payload that you sent through your HTTP request.

An HTTP request through the API Gateway gets transformed into an event object similar to this:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

As you can see, the JSON payload is accessible in a stringified form in event.body.

If you want to send the pass the same payload to the second Lambda, you have to parse it first.

const body = JSON.parse(event.body)

Then, send body instead of event.

Then, in your second Lambda, you parse the stringified JSON in event.body and then you get your original payload back.

If you sent name in that original payload, you can get it from JSON.parse(event.body).name.

Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format

I had some very similar problem. When trying to call JSON.parse() on the event.body, I'm getting an error because event is already the object that I'm trying to POST. I haven't actually linked my database to my front end, but I have this error while testing from Lambda and from API Gateway.

Example of how I'm being able only now to read the object from my request:

"use strict";
const AWS = require("aws-sdk");

exports.handler = async (event, context) => {

const documentClient = new AWS.DynamoDB.DocumentClient();

let responseBody; 
let statusCode;

console.log(event.id) // I have access to dot notation straight from the event

const {id, item, quantity, orderTotal, userId} = event;
Related