Web Services 如何在node.js中使用node-soap或strong-soap添加soap头

sf6xfgos  于 2022-11-24  发布在  Node.js
关注(0)|答案(2)|浏览(169)

我尝试在节点中使用xml web service soap客户端,但我不确定如何为我的示例添加soap头。
看看strong-soap,有一个方法addSoapHeader(value, qname, options),但我不确定在本例中需要传入什么作为qname和options。
我需要发送的请求

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

值应该是:

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

那么qname应该是什么?auth:AuthenticationHeader?在哪里指定名称空间?
有没有更简单的node-soap示例?我应该使用strong-soap还是node-soap?

kknvjkwl

kknvjkwl1#

我通过通读代码库找到了这样做的方法。(strong-soap
qname-限定名称
对于简单标题

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

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

对于像您这样的复杂头,直接在xml中指定它

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

eoxn13cs2#

以防有人来这里寻找答案:

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',
  )
);

相关问题