1

All,

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.

Here is are my code chunks.

HTML:

                <div class="row">
                <div class="col-md-12" class="pull-left">
                    <p> TEST </p>
                    <p>{{soapreply}}</p>
                </div>

JS:

  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('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">' +
  '<soapenv:Header/>' +
  '<soapenv:Body>' +
  '<ns:listCss sequence="?">' +
  '<searchCriteria>' +
  '<name>%</name>' +
  '</searchCriteria>' +
  '<returnedTags uuid="?">' +
  '<name>?</name>' +
  '<description>?</description>' +
  '<clause>?</clause>' +
  '</returnedTags>' +
  '</ns:listCss>' +
  '</soapenv:Body>' +
  '</soapenv:Envelope>');

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

}); }

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".

Much appreciated for any help you might have!

1 Answer 1

2

You're not waiting for your request to respond before you call res.render(), so the value of soapreplyx is always '', its initial value. To correct this, add an 'end' event listener on the response object passed to your https.request() callback.

You're not appending the chunks of the response to your soapreplyx variable, you're reassigning its value with each successive chunk.

let soapRequest = https.request(options, soapResponse => {
  soapResponse.on('data', chunk => {
    soapreplyx += chunk
  })

  soapResponse.on('end', () => {
    return res.render('cucmmapper-results.html', {
      title: 'CUCM 2.1',
      soapreply: soapreplyx
    })
  })
})

soapRequest.write(soapBody)
soapRequest.end()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! You hit the nail on the head. I got into the weeds and couldn't see my way back out. Thanks so much for your time helping me!

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.