aws lambda调用外部API在工作时显示错误

时间:2018-04-24 07:05:16

标签: node.js api amazon-web-services aws-lambda

使用lambda调用外部API ....使用xml文件的JSON填充POST。

API调用:https:serverName / api / SyncPersonnelViaAwsApi / SapEaiCall

点击API函数并返回正确的消息'不是最新版本的文件,更新未执行'

然而lambda说它失败了。

//// POST api/<controller>
  public string SapEaiCall([FromBody]string xmlFile)
    {
        string responseMsg = "Failed Import Active Directory User";

        if (string.IsNullOrEmpty(xmlFile))
        {
            responseMsg = "XML file is NULL";
        }

        if (responseMsg != "XML file is NULL")
        {
            xmlFile = RemoveFirstAndLastQuotes(xmlFile);

            if (!IsNewestVersionOfXMLFile(xmlFile))
            {
                responseMsg = "Not latest version of file, update not performed";
            }
            else
            {
                Business.PersonnelReplicate personnelReplicate = BusinessLogic.SynchronisePersonnel.BuildFromDataContractXml<Business.PersonnelReplicate>(xmlFile);
                bool result = Service.Personnel.SynchroniseCache(personnelReplicate);

                if (result)
                {
                    responseMsg = "Success Import Sap Cache User";
                }
            }
        }

        return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";

    }

这是日志中给出的所有数据......但这是正确的消息回发...有没有人知道为什么它仍然标记为失败?

(下面的代码)

   var querystring = require('querystring');
var https = require('https');
var fs = require('fs');

exports.handler = function(event, context) {

  const post_data = JSON.stringify('="xmlData"');

    // An object of options to indicate where to post to
    var post_options = {
        host: 'ServerName',
        protocol: 'https:',
       // port: '443',
        path: '/api/SyncPersonnelViaAwsApi/SapEaiCall',
        method: 'POST',
        json:post_data,
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': post_data.length
        }
    };

   process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    var post_request = https.request(post_options, function(res) {
        var body = "";

        res.on('data', function(chunk)  {
            //chunk = '456';
            body += chunk;
        });

        res.on('end', function() {
            context.done(body);
        });

        res.on('error', function(e) {
            context.fail('error:' + e.message);
        });
    });

    // post the data
    post_request.write(post_data);
    post_request.end();
    console.log("posted data " +post_data);
};

(拉姆达:)

{{1}}

1 个答案:

答案 0 :(得分:2)

context.done() takes two parameters。第一个是错误对象,第二个是成功的响应对象。改变

res.on('end', function() {
  context.done(body); 
});

res.on('end', function() {    
  context.done(null, body); 
});