How to set headers using node-sapap in node.js

I am trying to use the 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'm trying to use requires a username and password through the headers.

Thanks in advance

+5
source share
6 answers

Now it may not be useful, however, to answer this question, which is still open, here it is.

You can use the Client.addSoapHeader method. According to the documentation

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

Functions

soapHeader Object ({rootName: {name: "value" }}) xml- , arg :

name ( )

xml

xmlns URI

, , :

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

README node-soap, , , WS-Security ( , SOAP), , .

WS-Security, README.

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

. . SOAPUI. , , , .

0

It worked for me. To create a wsdl definition, the create client method needs wsdl_header. After creating it, you need to establish basic 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); 
    }
0
source

I had the same problem and solved it below, this is what worked for me My wsdl with which I inserted data using the SOAPUI client (to find out which fields are 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 that I called in the 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)
        }
    })
})

 }
0
source

According to 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 

}
0
source

All Articles