用Node-soap(node.js)中的数组发送请求
我使用nodejs和node-soap与Web服务进行通信。 但我似乎无法获得将parameter passing给服务的语法。
该文件说,我需要发送一个数组的字段uuid和它的值。
这里是我从Web服务所有者那里得到的PHP代码
$uuid = "xxxx"; $param = array("uuid"=>new SoapVar($uuid, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema") )
这里是我在我的节点服务器中使用的代码
function getSoapResponse() { var soap = require('soap'); var url = 'http://live.pagoagil.net/soapserver?wsdl'; var auth = [{'uuid': 'XXXXXXXXX'}]; soap.createClient(url, function(err, client) { client.ListaBancosPSE(auth, function(err, result) { console.log(result); console.log(err); }); });
有了这个我得到不良的XML错误
var auth = [{'uuid': 'XXXXXXXXX'}];
要么
var auth = [["uuid",key1],XSD_STRING,"string","http://www.w3.org/2001/XMLSchema"];
并与此我得到的答复“用户名为空”(uuid)
var auth = {'uuid': 'XXXXXXXXX'};
有什么build议么?
我可以为你做的事情不多,但是这里有一些提示让你开始。
- 使用client.describe()来查看服务期望的参数。
您尝试访问的服务具有以下结构:
{ App_SoapService: { App_SoapPort: { Autorizar: [Object], AutorizarAdvance: [Object], AutorizarIac: [Object], ListaBancosPSE: [Object], AutorizarPSE: [Object], AutorizarTuya: [Object], AutorizarBotonCredibanco: [Object], FinalizarPSE: [Object], FinalizarTuya: [Object], ConsultarReferencia: [Object] } } }
仔细看一下ListaBancosPSE的具体方法,它提供了这个信息:
{input: { auth: 'soap-enc:Array' }, output: { return: 'soap-enc:Array' }}
我试过这个:
var soap = require('soap'); function getSoapResponse(url, auth) { soap.createClient(url, function(err, client) { console.log(client.describe()); console.log(client.describe().App_SoapService.App_SoapPort.ListaBancosPSE); client.ListaBancosPSE(auth, function(err, result) { console.log(JSON.stringify(result)); console.log(err); }); }); } getSoapResponse('http://live.pagoagil.net/soapserver?wsdl', {'soap-enc:Array' : {'uuid': 'XXXXXXXXX'}});
答案是相同的“尼加达,错误的使用情况,没有一个pudo autenticar en pagoagil.net”。
接下来的步骤是确定服务期望的消息。
可能是这样的:
<tns:ListaBancosPSE><uuid>XXXXXXXXX</uuid></tns:ListaBancosPSE>
要么
<tns:ListaBancosPSE><soap-enc:Array><uuid>XXXXXXXXX</uuid></soap-enc:Array></tns:ListaBancosPSE>
一旦你知道了,你只需要在你安装的node-soap软件包中添加一个console.log文件,那么去你安装了node_modules的地方,然后打开这个文件
node_modules /肥皂/ LIB / client.js
在第187行添加一个console.log,在消息被设置之后
console.log("Message! ", message);
这将显示消息,这应该给你足够的信息来找出参数的格式。
最后使用这个答案中的内容和修改soap节点模块中的代码我能够获得我需要的代码。
我需要这样的东西:
<auth xsi:type="ns2:Map"> <item> <key xsi:type="xsd:string">uuid</key> <value xsi:type="xsd:string">{XXXXXX}</value> </item> </auth>
所以我用这个来创build参数:
var arrayToSend= {auth : [ { 'attributes' : {'xsi:type':"ns2:Map"}, 'item': [ {'key' : {'attributes' : { 'xsi:type': 'xsd:string'}, $value: 'uuid' } }, {'value' : {'attributes' : { 'xsi:type': 'xsd:string'}, $value: uuid } } ] } ] };
并发送它像这样:
soap.createClient(url, myFunction); function myFunction(err, client) { client.ListaBancosPSE(arrayToSend,function(err, result) { console.log('\n' + result); }); }
然后棘手的部分是学习wsd.js
所以它没有添加额外的标签,每次我使用和数组。 我去了1584行,并改变了这个:
if (Array.isArray(obj)) { var arrayAttr = self.processAttributes(obj[0]), correctOuterNamespace = parentNamespace || ns; //using the parent namespace if given parts.push(['<', correctOuterNamespace, name, arrayAttr, xmlnsAttrib, '>'].join('')); for (var i = 0, item; item = obj[i]; i++) { parts.push(self.objectToXML(item, name, namespace, xmlns, false, null, parameterTypeObject, ancXmlns)); } parts.push(['</', correctOuterNamespace, name, '>'].join('')); }
基本上现在它不会在每个迭代中推开启和closures标签,而只是在整个循环之前和之后。
另外我需要添加消息的xlmns的定义。 Client.js:186
xml = "<soap:Envelope " + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema"' + 'xmlns:ns2="http://xml.apache.org/xml-soap"' + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
希望这可以帮助使用这个图书馆的人在这种情况下。