13

I want to access a WSDL service through SOAP Client in Node.js. I used soap node module. But I can't able to find any documentation to set username and password. I'm not going to create SOAP server, I just want SOAPClient which is similar to PHP's SoapClient, using which I should able to access the WSDL service.

Update:

I had forked and customised the source to support this feature https://github.com/sincerekamal/node-soap

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

5 Answers5

26

You can provide username and password like this:

var soap = require('soap');
var url = 'your WSDL url';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");

soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});

(derived from https://github.com/vpulim/node-soap/issues/56, thank you Gabriel Lucena https://github.com/glucena)

iloo
  • 926
  • 12
  • 26
11

Another option to add basic authentication is using client.addHttpHeader. I tried both setSecurity and setting wsdl_headers but neither worked for me when authenticating to Cisco CUCM AXL.

Here is what worked for me:

var soap = require('soap');
var url = 'AXLAPI.wsdl';  // Download this file and xsd files from cucm admin page
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url,function(err,client){
  client.addHttpHeader('Authorization',auth);
});
10

Just to share what I've read from https://github.com/vpulim/node-soap:

var soap = require('soap');
var url = 'your WSDL url';

soap.createClient(url, function(err, client) {
    client.setSecurity(new soap.BasicAuthSecurity('your username','your password'));
});
Simon Elbaz
  • 109
  • 1
  • 6
  • 2
    Wouldn't this error if the wsdl was also behind authentication? – Ed Bishop Jan 21 '16 at 13:57
  • From what I have experienced, no. Seems that authentication is done before getting the WSDL. – Simon Elbaz Jan 21 '16 at 16:48
  • 3
    No, the wsdl is used to build the client that is passed to callback where you are then setting the security on it. As far as I can tell this method is there for when the wsdl does not require auth, requires a different auth or is loaded from the file system. I think you need to pass the auth to the create client method: var auth = "Basic " + new Buffer('username' + ':' + 'password').toString("base64"); var client = Soap.createClient('wsdlUrl', { wsdl_headers: { Authorization: auth } }, (err, client) => { if (err) { throw err; } else { client.yourMethod(); } }); – Ed Bishop Jan 23 '16 at 09:45
5

You need to set the username and password by passing the authorisation to the wsdl_headers object e.g

var auth = "Basic " + new Buffer('username' + ':' + 'password').toString("base64");

var client = Soap.createClient('wsdlUrl', { wsdl_headers: { Authorization: auth } }, (err, client) => {
    if (err) {
        throw err;
    } else {
        client.yourMethod();
    }
});
Ed Bishop
  • 770
  • 8
  • 19
5

A small tweak to the existing answers: you can use your security object to create the header for the WSDL request too, e.g.

const security = new soap.BasicAuthSecurity(username, password);
const wsdl_headers = {};
security.addHeaders(wsdl_headers);
soap.createClientAsync(url, { wsdl_headers }).then((err, client) => {
    client.setSecurity(security);
    // etc.
});

Or if you're using something more complicated than BasicAuthSecurity you may also need to set wsdl_options from the security object, e.g.

const security = new soap.NTLMSecurity(username, password, domain, workstation);
const wsdl_headers = {}, wsdl_options = {};
security.addHeaders(wsdl_headers);
security.addOptions(wsdl_options);
soap.createClientAsync(url, { wsdl_headers, wsdl_options }).then((err, client) => {
    client.setSecurity(security);
    // etc.
});
Rup
  • 33,765
  • 9
  • 83
  • 112