如何使用node-soap创建自定义请求

时间:2017-08-24 05:09:22

标签: javascript node.js xml soap wsdl

这是我需要发送给wsdl的消息:

<?xml version="1.0" encoding="UTF-8" ?>    
<soapenv:Envelope 
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:tem="http://tempuri.org/">
       <soapenv:Header/>
       <soapenv:Body>
          <tem:ConsultarCreditos>
             <tem:usuario>DEMO010233001</tem:usuario>
             <tem:password>Pruebas1a$</tem:password>
          </tem:ConsultarCreditos>
       </soapenv:Body>
    </soapenv:Envelope>

我有这段代码:

const wsdlOptions = {
    envelopeKey: "soapenv"
};
soap.createClient(URL, wsdlOptions, function(err, client) {
    const args = {
        _xml: '<tem:ConsultarCreditos><tem:usuario> DEMO010233001 </tem:usuario><tem:password>Pruebas1a$</tem:password></tem:ConsultarCreditos>',
    }
    client.ConsultarCreditos(args, function(err, result, raw, soapHeader) {
        console.log('last request: ', client.lastRequest)
    });

});

结果如下:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
    xmlns:tns="http://tempuri.org/" 
    xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract">
    <soapenv:Body>
        <tem:ConsultarCreditos>
            <tem:usuario>DEMO010233001</tem:usuario>
            <tem:password>Pruebas1a$</tem:password>
        </tem:ConsultarCreditos>
    </soapenv:Body>
</soapenv:Envelope>

我需要更改标记soapenv:Envelope的属性,但我不知道该怎么做。

我只需要这些属性:

> xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
> xmlns:tem="http://tempuri.org/"

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

可能这不是最佳解决方案,但它对我有用。 在回调中,createCliete会覆盖我想要的xmlns属性client.wsdl.xmlnsInEnvelopeclient.wsdl.xmlnsInEnvelope = 'xmlns:tem="http://tempuri.org/"';

完整的代码:

    soap.createClient(URL, wsdlOptions, function(err, client) {
        client.wsdl.xmlnsInEnvelope = 'xmlns:tem="http://tempuri.org/"';
        const args = {
            _xml: '<tem:ConsultarCreditos><tem:usuario> DEMO010233001 </tem:usuario><tem:password>Pruebas1a$</tem:password></tem:ConsultarCreditos>',
        }
        client.ConsultarCreditos(args, function(err, result, raw, soapHeader) {
            console.log('last request: ', client.lastRequest)
        });

    });

结果:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tem="http://tempuri.org/">
    <soapenv:Body>
        <tem:ConsultarCreditos>
            <tem:usuario>DEMO010233001</tem:usuario>
            <tem:password>Pruebas1a$</tem:password>
        </tem:ConsultarCreditos>
    </soapenv:Body>
</soapenv:Envelope>