节点HTTPS请求没有在Lambda中返回回调函数

Node HTTPS request not returning callback function in Lambda

本文关键字:返回 回调 函数 Lambda HTTPS 请求 节点      更新时间:2023-09-26

我在AWS Lambda中使用节点的请求包,我试图在请求的回调函数中获得成功请求的正文。我知道请求是成功的,因为它在Toggl中创建了资源,但它没有显示在日志中。下面的设置工作在其他Express/Node应用程序我有,但不是在这里?我刚来Lambda,所以可能我错过了一些明显的东西。Lambda说console.log()将记录到Cloudwatch,但这就像回调函数没有运行一样。什么好主意吗?

var request = require('request');
exports.handler = function signupNew(event, context){
  
  request({'url': 'https://www.toggl.com/api/v8/projects', //URL to hit
          'method': 'POST',
          'headers': {
              'Content-Type': 'application/json',
              'Accept': 'application/json'
          },
          'auth': {
            'user': 'xxxxx',
            'pass': 'api_token'
          },
    'json': {
      "project":{
      "name": "Bobby", //name of project,
      "wid":1057436, //Workspace
      "template_id":16368482, //template
      "is_private":false //public?
    }
      }, function(error, response, body){ //response for API call 
          if(error) {
              console.log(error);
          } else {
              //signupPayload.toggl = body;
              console.log(response.statusCode, body);
              context.done(null, body)
          }
      }});
} //end of handler function

原来我在回调函数和请求之间缺少了一个'}'。愚蠢的小错误,但至少现在有一个关于如何使用请求模块在Lambda上执行POST请求的好例子。