Vartan Arabyan
Vartan Arabyan

Reputation: 3073

how to set headers using node-soap in node.js

I am trying to consume a wsdl service and found node-soap, but I cannot find how to set some headers.

Example :

header = {
  "Username": "foo",
  "Password" : "bar"
}

The reason I need this is because the wsdl I am trying to consume requires the username and password via the headers.

Thanks in advance

Upvotes: 7

Views: 26086

Answers (6)

EdwinCab
EdwinCab

Reputation: 411

According with the documentation , for aggregate HTTP Headers, you can put headers, example code:

soap.createClient(url, 

    function (err, client) {
      if(err){
          console.log(err);
      } else{
          console.log(client.describe())
          var soapHeaders = {
              'Channel':'PB',
              'RqUID':'1987'
          }
          client.addHttpHeader('<nameH1>', 'valueH1');
          client.addHttpHeader('<nameH2>', 'valueH2');
//then, continue calling the soap operation 

}

Upvotes: 2

Apoorv
Apoorv

Reputation: 620

I had the same issue and i resolved it in the below is what worked for me My wsdl with which i inserted data using SOAPUI Client(To see what are the fields required)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:yourUrn">
<soapenv:Header>
  <urn:AuthenticationMethod>
     <urn:userName>username</urn:userName>
     <urn:password>password</urn:password>
   </urn:AuthenticationMethod>
    </soapenv:Header>
    <soapenv:Body>
    <urn:SoapFunctionToCall>
     <urn:Field1>Text</urn:Field1>
      <urn:Field2>Text</urn:Field2>
       <urn:Field3>Text</urn:Field3>
        <urn:Field14>Text</urn:Field4>
         <urn:Field5>Text</urn:Field5>
         <urn:Field6>Text</urn:Field6>
      </urn:SoapFunctionToCall>
     </soapenv:Body>
    </soapenv:Envelope>

Below is the method i called in node

function createSoapEntry(){
let url = "your wsdl url"
var credentials = {
    AuthenticationMethod:{
        userName: "username",
        password: "password"
    }  
}
let args = { 
            Field1:"Text",
            Field2:"Text",
            Field3:"Text",
            Field4:"Text",
            Field5:"Text",
            Field6:"Text"            
    }
soap.createClient(url, function (err, client){
    client.addSoapHeader(credentials)

    client.SoapFunctionToCall(args, function (err, res) {
        if (err) {
            console.log("Error is ----->" + err)
        } else {
            console.log("Response is -----> " + res)
        }
    })
})

 }

Upvotes: 0

Leonardo Ampuero
Leonardo Ampuero

Reputation: 381

This worked for me. The create client method needs the wsdl_header to retrieve the wsdl definition. Once it is created, you need to set the basic auth security.

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

soap.createClient( url,{ wsdl_headers: {Authorization: auth} }).then(
    function(client){
        client.setSecurity(new soap.BasicAuthSecurity('rflhconfort_ws', '6syqxje9'));
        client.your_Method(args); 
    }

Upvotes: 0

Pradeep Kumar Aitha
Pradeep Kumar Aitha

Reputation: 11

soap = require('soap')
parseString = require('xml2js').parseString

soap.createClient('https://api.bingads.microsoft.com/Api/Advertiser/AdIntelligence/v9/AdIntelligenceService.svc?wsdl', function(err, client) {
  var soapHeader = {
    'AuthenticationToken': process.argv[2],
    'DeveloperToken': process.argv[3],
    'CustomerId': process.argv[4],
    'CustomerAccountId': process.argv[5]
  };
  client.addSoapHeader(soapHeader);
  client.SuggestKeywordsFromExistingKeywords({Keywords: ["Hello world"]}, function(err, result) {
    console.log(result.body);
  });
});

This won't work. The reply is invalid login credentials. The same credentials work fine with SOAPUI. So, the format of sending the login credentials mentioned in the other answers must be wrong.

Upvotes: 0

V31
V31

Reputation: 7666

It may not be useful now however inorder to answering this question which is still open, here it goes.

You can make use of the method Client.addSoapHeader. As per the documentation

Client.addSoapHeader(soapHeader[, name, namespace, xmlns]) - add soapHeader to soap:Header node

Options

soapHeader Object({rootName: {name: "value"}}) or strict xml-string Optional parameters when first arg is object :

name Unknown parameter (it could just a empty string)

namespace prefix of xml namespace

xmlns URI

So you need to create an object and pass that to this method like:

var soapHeader = {
  "Username": "foo",
  "Password" : "bar"
};
client.addSoapHeader(soapHeader);

Upvotes: 11

user1207456
user1207456

Reputation:

Reading through the README for node-soap, if what you're trying to do is not WS-Security (I have no idea because I stay far away from SOAP), then you're going to have to file an issue with the author, because I see no way to set custom headers according to the documentation.

If it is WS-Security, then follow the instructions on this part of the README.

Upvotes: 2

Related Questions