gpt4 book ai didi

javascript - Node.js http.request 结果作为变量返回

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:58 26 4
gpt4 key购买 nike

全部,

我试图弄清楚如何将 node.js 代码中 https.request 的结果传递给变量。我有一个 https.request 设置,可以正确地将正确的信息传递到 SOAP API 并获取正确的响应。我的最终目标是将 https.request 的输出获取到一个可以使用 Express 调用的变量中。

这是我的代码块。

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

});}

“console.log("Got Data: "+ d)”行正在从 API 获取正确的预期回复,但是,我不知道如何将该数据放入我的变量“soapreplyx”中,该变量在 Express 中更改为“soapreply”。

非常感谢您提供的任何帮助!

最佳答案

在调用 res.render() 之前,您不会等待请求响应,因此 soapreplyx 的值始终为 '',其初始值。要纠正此问题,请在传递给 https.request() 回调的响应对象上添加一个 'end' 事件监听器。

您没有将响应 block 附加到您的 soapreplyx 变量,而是为每个连续的 block 重新分配其值。

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

关于javascript - Node.js http.request 结果作为变量返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46593847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com