javascript - 在 AWS Lambda 中获取正文请求

标签 javascript node.js lambda

我有以下运行 NodeJs 8.0 的 AWS Lambda,它接收来自 API Gateway 的请求。

Lambda 代码如下所示:

const mysql = require('mysql');

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

这一行:

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

打印以下内容(在 Cloudwatch 中)

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

我想获取 requestType 的值,因此尝试执行以下操作:

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

但它打印

event.body.createdAt = undefined

如何获取 requestType 值?

最佳答案

我认为,正文是一个编码字符串,试试这个:

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

关于javascript - 在 AWS Lambda 中获取正文请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090257/

相关文章:

javascript - File 和 FileReader 对象没有方法

mysql - 序列化 findAll,其中列值与表中至少一项其他项目相同

java - 使用匿名可运行类代码进入死锁状态,但使用 lambda 可以正常工作

c# - c# 表达式上的奇怪类型转换

javascript - div 向相反方向滚动

javascript - Ajax 不支持表单标签

javascript - Bootstrap 选项卡内容/选项卡 Pane 上的 ng-repeat

javascript - 响应 cookie 未在 Fetch 请求中发送

javascript - Nodejs 中的 Passport 身份验证

json - 只读文件系统: 'data.json' : IOError in aws lambda function