简体   繁体   English

AWS Lambda调用外部API时显示错误

[英]aws lambda calling external API showing error when it is working

Using a lambda calling an external API....Populating the POST with a JSON of an xml file. 使用调用外部API的lambda。...使用xml文件的JSON填充POST。

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

Hits the API function and returns the correct message 'Not latest version of file, update not performed' 点击API函数并返回正确的消息“不是最新版本的文件,未执行更新”

However the lambda says it has failed. 但是,lambda表示它已失败。

    Response:
{
  "errorMessage": "\"{'response' : 'Not latest version of file, update not performed'}\""
}

this is all the data that is given in the logs...but this is the correct message postback...does anyone have any idea why it is still flagging as a fail? 这是日志中给出的所有数据...但这是正确的消息回发...没有人知道为什么它仍然标记为失败吗?

(code below) (下面的代码)

//// 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 \"}";

    }

(lambda:) (lambda :)

   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);
};

context.done() takes two parameters . context.done()具有两个参数 The first one is the error object and the second is response object on success. 第一个是错误对象,第二个是成功对象的响应对象。 Change 更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda 超时(NodeJS - 调用 Google Calendar API 时) - AWS Lambda Timeout (NodeJS - when calling Google Calendar API) 调用 UpdateFunctionCode 操作 Lambda AWS 时发生错误 (MissingAuthenticationTokenException) - An error occurred (MissingAuthenticationTokenException) when calling the UpdateFunctionCode operation Lambda AWS AWS Lambda:如何将秘密存储到外部API? - AWS Lambda: How to store secret to external API? 从 Lambda 调用外部 api 端点时出现 ENOTFOUND 错误 - ENOTFOUND error when call external api endpoint from Lambda AWS Lambda连接到外部REDIS实例无法正常工作 - AWS Lambda connection to external REDIS instance not working AWS Lambda 不在循环中调用第三方 API - AWS Lambda not calling third party api in loop AWS Lamda api 无法从浏览器显示语法错误 - AWS Lamda api not working from browser showing syntax error next.js api 路由,调用一些外部 api Z572D1221E8E6BBC1E421E5E6BBC1E421E5E6BBC1E421E5E6BBC79 - next.js api route, 504 timeout in aws EC2 when calling some external api url aws lambda - 使用 axios 调用第三方 API 时 503 服务不可用 - aws lambda - 503 service unavailable when calling 3rd party API using axios 使用`requestjs`从Node Js调用AWS lambda端点时总是出现502错误 - Always get 502 error when calling AWS lambda endpoint from Node Js using `requestjs`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM