title: aws lambda call not populating body on POST
body: <p>Thanks to help from EVK on my previous Q (<a href="https://stackoverflow.com/questions/49665248/can-use-api-get-but-not-api-post">can use API GET but not API POST</a>) I was able to resolve the problem of not being able to make an API POST request from an aws lambda in node, when I could hit the GET. The problem was not populating post_options.</p>
  
  <p>Anyway, now I am able to successfully call the post but I cant get the body populated.</p>
  
  <p>related documentation <a href="https://nodejs.org/api/http.html#http_http_request_options_callback" rel="nofollow noreferrer">https://nodejs.org/api/http.html#http_http_request_options_callback</a></p>
  
  <p>If you see my API POST call below.  </p>
  
  <pre><code> //// POST api/&lt;controller&gt;
        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&lt;Business.PersonnelReplicate&gt;(xmlFile);
                      bool result = Service.Personnel.SynchroniseCache(personnelReplicate);
  
                      if (result)
                      {
                          responseMsg = "Success Import Sap Cache User";
                      }
                  }
              }
  
              return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";
  
          }
  </code></pre>
  
  <p>Every time I call it from the aws lambda, it returns <code>responseMsg = "XML file is NULL";</code></p>
  
  <p>Please see my example below:</p>
  
  <pre><code>    var querystring = require('querystring');
  var https = require('https');
  var fs = require('fs');
  
  exports.handler = function(event, context) {
  
     const post_data = querystring.stringify({'msg': 'Hello World!'});
  
      // An object of options to indicate where to post to
      var post_options = {
          host: 'URL',
          protocol: 'https:',
          port: '443',
          path: '/api/SyncPersonnelViaAwsApi/SapEaiCall',
          method: 'POST',
          headers: {
              'Content-Type': 'application/json',
              'Content-Length': post_data.length
          }
      };
  
      //ignores SSL
     process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
      var post_request = https.request(post_options, function(res) {
          var body = "";
  
          res.on('data', function(chunk)  {
              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); //returns posted data msg=Hello%20World!
  };
  </code></pre>
  
  <p>So I have populated post data, and also tried populating the body.  Still returns <code>XML file is NULL</code></p>
  
  <p>Does anyone please have any idea why?</p>
  
  <p>Thanks</p>