How can I force namespace prefix usage?

7.2k views Asked by At

I'm trying to consume a SOAP Webservice, but the WSDL is kind of broken, so I have to do some customization to node-soap.

The ideal SOAP Envelope that I would like to have would be this one:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <getImagesDefinition xmlns="http://services.example.com/"/>
    </Body>
</Envelope>

So far this is the nodejs code I have to invoke the service:

var soap = require('soap');
var url = 'http://www.example.com/services/imagesizes?wsdl';

soap.createClient(url, function(err, client) {

    client.setEndpoint('http://www.example.com/services/imagesizes');
    client.getImagesDefinition(null, function(err, result) {
        console.log(result);
    });
    console.log(client.lastRequest)

}); 

I had to set the endpoint manually because it is broken in the WSDL file

The envelope I get when printing client.lastRequest is this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
               xmlns:tns="http://services.example.com/">
    <soap:Body>
        <getImagesDefinition />
    </soap:Body>
</soap:Envelope>

I know that if I can force the namespace prefix on the body to have <tns:getImagesDefinition /> instead of <getImagesDefinition /> the request works perfectly.

Is there any way for me to force it?

I read the documentation saying that tns is a default ignored namespace, so I tried to change that by doing this:

var options = {
    ignoredNamespaces: {
        namespaces: [],
        override: true
    }
}

and sending that object to the soap.createClient method, but I see no difference on the Envelope.

Is there anyway for me to force this? or get to the ideal SOAP Envelope?

Thanks!

2

There are 2 answers

3
Simon On

See this thread discussing the same issue at github:

And especially https://github.com/vpulim/node-soap/issues/537#issuecomment-72041420

0
Halfstop On

I ran into this exact problem and for me, the fix was to override the ignoredNamespaces - to remove 'tns' as an ignored namespace.

var options = { 
  ignoredNamespaces: {
    namespaces: ['targetNamespace', 'typedNamespace'],
    override: true
  }
}

I'm not sure why it didn't work for you, but maybe there was a bug in the library that has since been fixed. Or maybe because you didn't include any namespaces, but rather an empty array.