0

Using a lambda calling an external API....Populating the POST with a JSON of an xml file.

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

Hits the API function and returns the correct message 'Not latest version of file, update not performed'

However the lambda says it has failed.

    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:)

   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
    Lambda with show success if you use context.done and show error if you use context.fail. Commented Apr 24, 2018 at 7:17

1 Answer 1

2

context.done() takes two parameters. 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); 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.