My lamda function handler looks like this
export const handler = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
var data = event.body;
var id=data.companyid;//gets undefined
var orders = data.orders;//gets undefined
let response = {
statusCode: 200,
headers: {
},
body: ""
};
console.log("response: " + JSON.stringify(response))
callback(null, response);
};
The request is a post with json data.
I am not able to access the properties in the json object. When I try to get value of "companyid"or "orders", I get undefined.
my json request (short simplified version):
{"companyid":"31434","orders":["452455","486783","418784"]}
Why am I not able to access "companyid" or "orders".
additional notes:
1.Initially I tried var data = JSON.parse(event.body)
. But that does not work : gets "unexpected token in JSON at position" error. There is no problem with the request json. This is a working nodejs app that I am trying to convert to lambda.
console.log(event)
shows the below result
{
version: '2.0',
routeKey: 'ANY /stringspush',
rawPath: '/default/stringspush',
rawQueryString: '',
headers: {
accept: '*/*',
'accept-encoding': 'gzip, deflate, br',
'cache-control': 'no-cache',
'content-length': '295686',
'content-type': 'application/json',
host: '7x8hl42b61.execute-api.us-west-1.amazonaws.com',
'postman-token': '2c029ae3-6911-41af-b643-771d6273123a',
'user-agent': 'PostmanRuntime/7.28.4',
'x-amzn-trace-id': 'Root=1-61e58198-762e4002234732094ad50029',
'x-forwarded-for': '122.171.25.249',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https'
},
requestContext: {
accountId: '811697223032',
apiId: '7x8hl42b61',
domainName: '7x8hl42b61.execute-api.us-west-1.amazonaws.com',
domainPrefix: '7x8hl42b61',
http: {
method: 'POST',
path: '/default/stringspush',
protocol: 'HTTP/1.1',
sourceIp: '122.171.25.249',
userAgent: 'PostmanRuntime/7.28.4'
},
requestId: 'MGEwBj74yK4EJqA=',
routeKey: 'ANY /stringspush',
stage: 'default',
time: '17/Jan/2022:14:47:53 +0000',
timeEpoch: 1642430873544
},
body: `{"companyid":"5287","strings":["5452245", "7564534"]}`,
isBase64Encoded: false
}
- console.log(event.body)
{"companyid":"5287","strings":["5452245", "7564534"]}
console.log output added to question