1

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?

katronai
  • 540
  • 1
  • 8
  • 25

3 Answers3

3

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);
    }
});
katronai
  • 540
  • 1
  • 8
  • 25
0

add this code client.setSecurity(new soap.BasicAuthSecurity('username', 'password')); after your create client. It worked for me:

var soap = require('soap');
var url = 'http://myservice.com/MyService.svc?wsdl';
soap.createClient(url, function(err, client) {
if (!err) {      
      client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));
      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);
}
});
0

I have updated npm soap version and it worked for me

Nugzar Gaguliya
  • 146
  • 1
  • 13