[英]How to add soap header using node-soap or strong-soap in node.js
我正在嘗試在節點中使用 xml web 服務 soap 客戶端,但我不確定如何為我的示例添加 soap header。
查看 strong-soap,有一個方法addSoapHeader(value, qname, options)
但我不確定在這種情況下我需要作為 qname 和選項傳入什么。
我需要發送的請求
<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?
我通過閱讀代碼庫找到了做到這一點的方法。 ( 強肥皂 )
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>`
);
以防萬一有人來這里尋找答案:
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.