简体   繁体   English

如何在 node.js 中使用 node-soap 或 strong-soap 添加 soap header

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

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.我正在尝试在节点中使用 xml web 服务 soap 客户端,但我不确定如何为我的示例添加 soap header。

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.查看 strong-soap,有一个方法addSoapHeader(value, qname, options)但我不确定在这种情况下我需要作为 qname 和选项传入什么。

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?那么qname应该是什么呢? auth:AuthenticationHeader ? auth:AuthenticationHeader Where do I specify the namespace?我在哪里指定命名空间?

Is there an easier example with node-soap? node-soap 有更简单的例子吗? Should I use strong-soap or node-soap?我应该使用 strong-soap 还是 node-soap?

I found the way to do that by reading through the codebase. 我通过阅读代码库找到了做到这一点的方法。 ( strong-soap ) 强肥皂

qname - qualified name qname - 限定名称

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 对于像你这样的复杂标题,直接在xml中指定它

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM