node.jsweb-servicessoapnode-soap

Cannot parse response error while consuming SOAP service with node.js


I am trying to consume a SOAP web service using node-soap module. However, I get 'Cannot parse response' error while calling one of the method of the web service.

Here is the implementation:

var soap = require('soap');
var url = 'http://myservice.com/MyService.svc?wsdl';
var args = {
    Username: '***',
    Password: '***'
};

soap.createClient(url, function(err, client) {
    if (!err) {      
        client.MyService(args, function(err, response) {
            if (!err) {
                console.log('MyService response:', response);
            } else {
                console.log('Error in MyService:', err);
            }
        });
    } else {
        console.log('Error in createClient: ', err);
    }
});

How can I fix this?


Solution

  • I figured out the problem. While the web service expecting application/soap+xml content type, the content type was text/xml. So I added forceSoap12Headers: true among createClient() parameters to force the node-soap to use SOAP 1.2 version. Also, I added the ws-addressing headers to soap header because of The message with To ' ' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher error.

    The overall code:

    var soap = require('soap');
    var url = 'http://myservice.com/MyService.svc?wsdl';
    var args = {
        Username: '***',
        Password: '***'
    };
    var soapOptions = {
        forceSoap12Headers: true
    };
    var soapHeaders = {
        'wsa:Action': 'http://tempuri.org/MyPortName/MyAction',
        'wsa:To': 'http://myservice.com/MyService.svc'
    };
    
    soap.createClient(url, soapOptions, function(err, client) {
        if (!err) {
            client.addSoapHeader(soapHeaders, '', 'wsa', 'http://www.w3.org/2005/08/addressing');
            client.MyService(args, function(err, response) {
                if (!err) {
                    console.log('MyService response:', response);
                } else {
                    console.log('Error in MyService:', err);
                }
            });
        } else {
            console.log('Error in createClient: ', err);
        }
    });