title: Node.js http.request Results Back as Variable
body: <p>All,</p>
  
  <p>I am trying to figure out how to pass the results from an https.request in node.js code out to a variable. I have an https.request setup that correctly passes the correct information to a SOAP API and gets the correct response back. My ultimate goal is to get the output from the https.request into a variable that I can call using Express.</p>
  
  <p>Here is are my code chunks.</p>
  
  <p>HTML:</p>
  
  <pre><code>                &lt;div class="row"&gt;
                  &lt;div class="col-md-12" class="pull-left"&gt;
                      &lt;p&gt; TEST &lt;/p&gt;
                      &lt;p&gt;{{soapreply}}&lt;/p&gt;
                  &lt;/div&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>  app.post('/cucmmapper/submit', function (req, res) {
  // FORM - DATA COLLECTION
  var cucmpub = req.body.cucmpub;
  var cucmversion = req.body.cucmversion;
  var username = req.body.username;
  var password = req.body.password;
  var authentication = username + ":" + password;
  var soapreplyx = '';
  
  // SOAP - BUILD CALL
  var https = require("https");
  var headers = {
    'SoapAction': 'CUCM:DB ver=' + cucmversion + ' listCss',
    'Authorization': 'Basic ' + new Buffer(authentication).toString('base64'),
    'Content-Type': 'text/xml; charset=utf-8'
  };
  
  // SOAP - AXL CALL
  var soapBody = new Buffer('&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5"&gt;' +
    '&lt;soapenv:Header/&gt;' +
    '&lt;soapenv:Body&gt;' +
    '&lt;ns:listCss sequence="?"&gt;' +
    '&lt;searchCriteria&gt;' +
    '&lt;name&gt;%&lt;/name&gt;' +
    '&lt;/searchCriteria&gt;' +
    '&lt;returnedTags uuid="?"&gt;' +
    '&lt;name&gt;?&lt;/name&gt;' +
    '&lt;description&gt;?&lt;/description&gt;' +
    '&lt;clause&gt;?&lt;/clause&gt;' +
    '&lt;/returnedTags&gt;' +
    '&lt;/ns:listCss&gt;' +
    '&lt;/soapenv:Body&gt;' +
    '&lt;/soapenv:Envelope&gt;');
  
  // SOAP - OPTIONS
  var options = {
    host: cucmpub, // IP ADDRESS OF CUCM PUBLISHER
    port: 8443, // DEFAULT CISCO SSL PORT
    path: '/axl/', // AXL URL
    method: 'POST', // AXL REQUIREMENT OF POST
    headers: headers, // HEADER VAR
    rejectUnauthorized: false // REQUIRED TO ACCEPT SELF-SIGNED CERTS
  };
  
  // SOAP - Doesn't seem to need this line, but it might be useful anyway for pooling?
  options.agent = new https.Agent(options);
  
  // SOAP - OPEN SESSION
  var req = https.request(options, function (res) {
    res.setEncoding('utf8');
    res.on('data', function (d) {
      soapreplyx = d;
      console.log("Got Data: " + d);
    });
  });
  
  // SOAP - SEND AXL CALL
  req.write(soapBody);
  res.render('cucmmapper-results.html'), {
    'title': 'CUCM 2.1',
    'soapreply': soapreplyx
  };
  req.end();
  req.on('error', function (e) {
    console.error(e);
  });
  </code></pre>
  
  <p>});
  }</p>
  
  <p>The line "console.log("Got Data: " + d)" is getting the correct expected reply from the API, however, I can't figure out how to get that data into my variable "soapreplyx" which changes in Express to "soapreply".</p>
  
  <p>Much appreciated for any help you might have!</p>