Node-soap - add a header with multiple namespaces

1.5k Views Asked by At

I'm using node-soap to communicate with a service that expects a header with elements in different namespaces.

Using a tool like SoapUI the header should look like this:

```xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tcorHeaders="http://www.tcore.com/TcoreHeaders.xsd" xmlns:tcorTypes="http://www.tcore.com/TcoreTypes.xsd">
   <soapenv:Header>
      <tcorHeaders:sessionHeader>
         <tcorHeaders:sessionToken>
            <tcorTypes:primary>token1</tcorTypes:primary>
            <tcorTypes:secondary>token2</tcorTypes:secondary>
            <tcorTypes:expiration>2018-09-19T03:35:19.478Z</tcorTypes:expiration>
         </tcorHeaders:sessionToken>
      </tcorHeaders:sessionHeader>
   </soapenv:Header>
```

But if I do:

client.addSoapHeader({
  sessionHeader : { 
    sessionToken: {
        primary : 'token1', 
        secondary: 'token2', 
        expiration : '2018-09-19T03:35:19.478Z' }}, 
'', 'types', 'http://www.tcore.com/TcoreTypes.xsd');

Then the output will look like this:

```xml
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:headers="http://www.tcore.com/TcoreHeaders.xsd" xmlns:types="http://www.tcore.com/TcoreTypes.xsd">
    <soap:Header>
        <types:sessionHeader xmlns:types="http://www.tcore.com/TcoreTypes.xsd" xmlns="http://www.tcore.com/TcoreTypes.xsd">
            <types:sessionToken>
                <types:primary>token1</types:primary>
                <types:secondary>token2</types:secondary>
                <types:expiration>2018-09-19T03:35:19.478Z</types:expiration>
            </types:sessionToken>
        </types:sessionHeader>
    </soap:Header>
```

With everything assigned to types and nothing assigned to headers.

How can I set a header containing an object with 2 different namespaces?

1

There are 1 best solutions below

0
On

I found an inelegant solution from the docs

Using the override I manually created the header object like this:

dclient.addSoapHeader({ 'headers:sessionHeader': 
    { 'headers:sessionToken': 
        { 'types:primary': token.primary, 
          'types:secondary': token.secondary, 
          'types:expiration': token.expiration } } });

This works because node-soap is consistent with it's naming conventions, but you will have to run a dummy request and then view the sent xml (result[3] if debugging)