Search code examples
node.jssoapwsdl

Remove namespace prefix from node-soap server response


This is response from my SOAP server:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<tns:pingResponse>
<tns:name>e3499cd86f59508466e8c6fcd37aabc8</tns:name>
</tns:pingResponse>
</soap:Body>
</soap:Envelope>

What I need is this for a response inside a BODY:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<pingResponse> // <<<< no TNS
<name>e3499cd86f59508466e8c6fcd37aabc8</name> // <<<< no TNS
<pingResponse> // <<<< no TNS
</soap:Body>
</soap:Envelope>

I was trying a lot from stackoverflow and else what I found but can't figuarate to handle this :/

This is my wsdl:

<wsdl:definitions
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://localhost:8080/soap/server"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    name="Cos_Service_Soap"
    targetNamespace="http://localhost:8080/soap/server"
>
    <wsdl:binding name="Cos_Service_SoapBinding" type="tns:Cos_Service_SoapPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="ping">
            <soap:operation soapAction="http://localhost:8080/soap/server#ping" />
            <wsdl:input>
                <soap:body use="literal" namespace="http://framework.zend.com" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="http://framework.zend.com" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
<service name="Cos_Service_SoapService">
        <port name="Cos_Service_SoapPort" binding="Cos_Service_SoapBinding">
            <soap:address location="http://localhost:8080/soap/server" />
        </port>
    </service>
<message name="pingIn" />
    <message name="pingResponse">
        <part name="return" type="xsd:string" />
    </message>
    <message name="pingOut">
        <part name="return" type="xsd:boolean" />
        <part name="name" type="xsd:boolean" />
    </message>
</wsdl:definitions>

Function to start server/listen:

function startServer() {
    server.listen(port);
    server.on('error', onError); 
    server.on('listening', onListening);

    soap.listen(server, {
        path: '/soap/server',
        services: poService.service,
        xml: poService.xml,
    });
}

and service:

var poService = {
  Cos_Service_SoapService: {
    Cos_Service_SoapPort: {   
          ping: function(args) {  
              console.log(args);            
              return {'name': args.token};       
          },           
      } 
   }
}; 

If I remove: xmlns:tns="http://localhost:8080/soap/server from wsdl then tns: change to undefined:.

If I remove style="rpc" from <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> it shows errors (from this answer https://stackoverflow.com/a/70478488/8522950):

<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>SOAP-ENV:Server</soap:Value>
<soap:Subcode>…</soap:Subcode>
</soap:Code>
<soap:Reason>
<soap:Text>TypeError: Cannot read properties of undefined (reading &apos;outputName&apos;)</soap:Text>
</soap:Reason>
</soap:Fault>
</soap:Body>
</soap:Envelope>

I can't find anything on npm node-soap about removing this prefix or replace it from response.


Solution

  • I did a classic replace at serverSoap.on('response').. with a map

    soapServer.on('response', (res) => {
        const replaceMap = {
            'ns1:return': 'return',
            'tns:return': 'return',
        };
        res.result = res.result.replace(
            new RegExp(Object.keys(replaceMap).join('|'), 'g'),
            (m) => replaceMap[m]
        );
    });