gpt4 book ai didi

amazon-web-services - 如何使用 Lambda 在无服务器应用程序中读取 POST 参数?

转载 作者:行者123 更新时间:2023-12-04 15:17:56 26 4
gpt4 key购买 nike

我正在向无服务器部署的 Lambda 函数提交表单,这是 yml:

functions:
hello:
handler: handler.hello
events:
- http: POST hello

现在我的 hello 函数是:
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Se222rverless v1.0! Your function executed successfully!',
input: event,
}),
};

callback(null, response);
};

我可以在输出中看到变量已传递,但它们存储在 event.body 属性中,如下所示:
 "body":"email=test%40test.com&password=test12345"

现在我可以访问这个字符串,但我不能从中读取单个变量,除非我进行一些正则表达式转换,我相信在无服务器/aws 这样的现代堆栈中不会出现这种情况。

我错过了什么?如何读取单个变量?

最佳答案

看起来您的无服务器端点正在接收带有 Content-Type: application/x-www-form-urlencoded 的数据.您可以更新请求以使用 JSON 数据来访问 post 变量,就像访问其他 JavaScript 对象一样。

假设这不是一种选择;您仍然可以使用节点查询字符串模块访问您的帖子正文数据来解析请求的正文,这是一个示例:

const querystring = require('querystring');

module.exports.hello = (event, context, callback) => {

// Parse the post body
const data = querystring.parse(event.body);

// Access variables from body
const email = data.email;

...

}

请记住,如果帖子正文中的某些参数使用了无效 JavaScript 对象标识符的名称来使用方括号表示法,例如:
const rawMessage = data['raw-message'];

关于amazon-web-services - 如何使用 Lambda 在无服务器应用程序中读取 POST 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49289302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com