rap*_*dko 5 amazon-web-services serverless-framework serverless
我正在向无服务器部署的Lambda函数提交表单,这是yml:
functions:
  hello:
    handler: handler.hello
    events:
      - http: POST 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这样的现代堆栈中,情况并非如此。
我想念什么?如何读取各个变量?
小智 7
看起来您的无服务器端点正在使用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'];
| 归档时间: | 
 | 
| 查看次数: | 5083 次 | 
| 最近记录: |