How to add soap header using node-soap or strong-soap in node.js

5.5k views Asked by At

I'm trying to use an xml web service soap client in node and I'm not sure how to add the soap header for my example.

Looking at strong-soap, there is a method addSoapHeader(value, qname, options) but I'm not sure what I need to pass in as qname and options in this case.

My request that I need to send

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aut="http://schemas.foo.com/webservices/authentication" xmlns:hot="http://foo.com/webservices/hotelv3" xmlns:hot1="http://schemas.foo.com/webservices/hotelv3">
   <soapenv:Header>
      <aut:AuthenticationHeader>
         <aut:LoginName>foo</aut:LoginName>
         <aut:Password>secret</aut:Password>
         <aut:Culture>en_US</aut:Culture>
         <aut:Version>7.123</aut:Version>
      </aut:AuthenticationHeader>
   </soapenv:Header>
   <soapenv:Body>
      <hot:BookHotelV3>
         <!--Optional:-->
         <hot:request>
            <hot1:RecordLocatorId>0</hot1:RecordLocatorId>
            <!--Optional:-->
            <hot1:RoomsInfo>
               <!--Zero or more repetitions:-->
               <hot1:RoomReserveInfo>
                  <hot1:RoomId>123</hot1:RoomId>
                  <hot1:ContactPassenger>
                     <hot1:FirstName>Joe</hot1:FirstName>
                     <hot1:LastName>Doe</hot1:LastName>
                  </hot1:ContactPassenger>
                  <hot1:AdultNum>2</hot1:AdultNum>
                  <hot1:ChildNum>0</hot1:ChildNum>
               </hot1:RoomReserveInfo>
            </hot1:RoomsInfo>
            <hot1:PaymentType>Obligo</hot1:PaymentType>
         </hot:request>
      </hot:BookHotelV3>
   </soapenv:Body>
</soapenv:Envelope>

Should value be:

value = { LoginName:'foo', Password:'secret', Culture:'en_US', Version:7.123 }

Then what should qname be? auth:AuthenticationHeader? Where do I specify the namespace?

Is there an easier example with node-soap? Should I use strong-soap or node-soap?

2

There are 2 answers

1
Petar Najman On

Just in case somebody else comes here looking for answers:

const QName = require('strong-soap').QName;
client.addSoapHeader(
  {
    $value: {
      LoginName: 'foo',
      Password: 'secret',
      Culture: 'en_US',
      Version: '7.123',
    },
  },
  new QName(
    'http://schemas.foo.com/webservices/authentication',
    'AuthenticationHeader',
  )
);
0
code-jaff On

I found the way to do that by reading through the codebase. (strong-soap)

qname - qualified name

for simple headers

const QName = require('strong-soap').QName;

client.addSoapHeader({
    item: {
        key: 'api_key',
        value: apiKey
    }
}, new QName(nsURI, 'Auth'));

for complex headers like you have, specify it in xml directly

client.addSoapHeader(
    `<aut:Auth xmlns:aut="${nsURI}">
        <aut:LoginName>foo</aut:LoginName>
    </aut:Auth>`
);