[英]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.