7

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

2
  • 4
    Not a helpful comment but one of our senior php engineers used to say "WSDL integration is insanity"... Commented May 19, 2012 at 4:36
  • I have setup node-soap but now im having other issues within addChild Commented May 19, 2012 at 10:00

6 Answers 6

11

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);
Sign up to request clarification or add additional context in comments.

1 Comment

addSoapHeader is creating new tags IN the header, but I want to create a different header ("env:Header", not "soap:Header"). Any idea how to do that?
2

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.

2 Comments

David it seem like you are right I have customized it and will push it soon. But I would like to state that node.js does not play well with soap at all.
I would say that's probably because SOAP is a nightmare standard borne out of everything that is wrong and evil in "enterprise" development.
2

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 

}

1 Comment

Thank youuuuuu -- I mean, I feel like this defeats the entire purpose of using SOAP, but my clients setup isn't perfect -- whos is? This feels like a mix of a few different technology stacks... But hey man, data flows.
0
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.

2 Comments

You have to add the 'tns:AuthenticationToken': <value> prefix for each value in soap headers
I had a similar issue, where it worked in SoapUI but not my node program. I had to use the optional parameter "xmlns" on the addSoapHeader() function, to match the prefixes on the header elements as seen in SoapUI. For instance: <soapenv:Header> <urn:SessionHeader> <urn:sessionId>datadatadata</urn:sessionId> <urn:SessionHeader> ...would be added to your soap call by using: client.addSoapHeader(soapHeader, undefined, undefined, "urn"); This is true even though the actual SoapAction function didn't require this specification, I'd think because it's defined in the wsdl.
0

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); 
    }

Comments

0

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)
        }
    })
})

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.