简体   繁体   English

在AWS Lambda中获取正文请求

[英]Get body request in AWS Lambda

I have the following AWS Lambda running NodeJs 8.0 which receives requests from API Gateway. 我有以下运行NodeJs 8.0的AWS Lambda,它从API网关接收请求。

Lambda code looks the following: Lambda代码如下所示:

const mysql = require('mysql');

exports.handler = (event, context, callback) => {
    console.log("event.body = " + event.body);
    console.log("event.body.requestType = " + event.body.requestType);
    .  
    .
    .
    .
}

This line: 这行:

console.log("event.body = " + event.body);

prints the following (in Cloudwatch) 打印以下内容(在Cloudwatch中)

2019-03-10T16:58:31.265Z    276b4902-e716-44b1-ad9e-ed4eb4e1c02d    event.body =
{
    "requestType": "single",
    "createdAt": "2019-03-10T16:58:29.722",
}

I want to get the value of requestType, so trying to do the following: 我想获取requestType的值,因此尝试执行以下操作:

console.log("event.body.requestType = " + event.body.requestType);

but it prints 但它打印

event.body.createdAt = undefined

How can I get the requestType value? 如何获取requestType值?

我认为,主体是编码字符串,请尝试以下操作:

console.log("event.body.requestType = " + JSON.parse(event.body).requestType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM