4

I am using Node.js 8.10 in lambda proxy integration. My goal is very simple.

  1. send json data to lambda function
  2. query rds with json data and retrive data from rds
  3. return response with json data in lambda from rds data.

but I faced issue in step 1. I tried to figure out and I asked aws support center. it was not helpful. please help me.

my test json data are :

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

my code is very simple but have a problem :

exports.handler = async (event) => {
    var body = JSON.parse(event)//<--this is a problem

    let responseBody = {
        message: "hello",
        key1: body.key1
    };
    const response = {
        statusCode: 200,
        headers : {"Access-Control-Allow-Origin" : "*"},
        body: JSON.stringify(responseBody)
    };
    return response;
};

I got this error in second line.

{"errorMessage":"Unexpected token o in JSON at position 1","errorType":"SyntaxError","stackTrace":["JSON.parse (<anonymous>)","exports.handler (/var/task/index.js:2:21)"]}

So I changed second line code like this.

var body = JSON.parse(JSON.stringify(event))

and then I got response well. I thought that's working well.

{
  "statusCode": 200,
  "headers": {
    "Access-Control-Allow-Origin": "*"
  },
  "body": "{\"message\":\"hello\",\"key1\":\"value1\"}"
}

but I sent same json data with postman for real. but I got only this.

{ "message": "hello" }

there isn't "key1". so I added console.log and then I checked log in cloudwatch.

{ message: 'hello', key1: undefined }

I can see aws editor log is well but when I send data with postman. lambda couldn't parse my json data. it means lambda couldn't parse event parameter.

my questions are : 1. when I tried to parse like this

var body = JSON.parse(event)"

why does lambda editor issue error? Is there something wrong? this is super simple and very common code.

  1. How can I parse my json data and return correct value not undefined. I expected this response

    { message: 'hello', key1: 'value1' }

I have been trying to solve this for 3days. but I really have no idea. please help me out.

1
  • It appears to me that the event parameter is not a JSON string as you expect. Rather it is a JavaScript object with several keys, including "statusCude", "headers" and "body". You probably need to look at event.body for the JSON you are expecting. Commented Mar 4, 2019 at 18:47

2 Answers 2

4

The body coming from API Gateway is a stringified JSON, so you need to parse it and then access the attribute you want.

'use strict';

exports.handler = async (event) => {
  let responseBody = {
    message: "hello",
    key1: JSON.parse(event.body).key1
  };
  const response = {
    statusCode: 200,
    headers: { "Access-Control-Allow-Origin": "*" },
    body: JSON.stringify(responseBody)
  };
  return response;

};

This will do it.

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

4 Comments

Thank you for your answer but when I test with postman, cloudwatch still show me key1 is undefined.
Thank you for your answer. but second question's point is I send json data with postman like this {"key1":"value1","key2":"value2","key3":"value3} as far as I know lambda event parameter get my json data. I want to use "key1"'s value. your second sample is not that I want.
I am very sorry. I think you misunderstood that's my fault. So I changed paragraph. anyway, thank you for your evidence. but I said that's working well in aws editor console. but when I send same data with postman. it's not work well. that's my problem. Can you test for me one more with postman? I appreciate you.
I feel grateful with your test and help. unfortunately, Your fourth code didn't work well both of postman, aws console. I got this error in aws console { "errorMessage": "Cannot read property 'key1' of undefined", "errorType": "TypeError", "stackTrace": [ "exports.handler (/var/task/index.js:6:26)" ] } and postman.
1

you need to pass the parameters in postman as JSON.

in the body tab choose the 'raw' option and then from the combo box select 'JSON' type: then just type :

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

and in your lambda function access to it like this:

JSON.parse(event.body).key1

Comments

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.