【问题标题】:how to set headers using node-soap in node.js如何在 node.js 中使用 node-soap 设置标头
【发布时间】:2012-05-18 21:01:24
【问题描述】:

我正在尝试使用 wsdl 服务并找到 node-soap,但我找不到如何设置一些标头。

例子:

header = {
  "Username": "foo",
  "Password" : "bar"
}

我需要这个的原因是因为我尝试使用的 wsdl 需要通过标头提供用户名和密码。

提前致谢

【问题讨论】:

  • 不是一个有用的评论,但我们的一位高级 php 工程师曾经说过“WSDL 集成是疯狂的”...
  • 我已经设置了 node-soap 但现在我在 addChild 中遇到了其他问题

标签: node.js soap express


【解决方案1】:

它现在可能没有用,但是为了回答这个仍然悬而未决的问题,就这样吧。

您可以使用 Client.addSoapHeader 方法。根据文档

Client.addSoapHeader(soapHeader[, name, namespace, xmlns]) - 添加 soapHeader 到soap:Header 节点

选项

soapHeader Object({rootName: {name: "value"}}) 或严格的 xml-string 第一个 arg 为 object 时的可选参数:

name 未知参数(可能只是一个空字符串)

xml命名空间的命名空间前缀

xmlns URI

因此您需要创建一个对象并将其传递给此方法,例如:

var soapHeader = {
  "Username": "foo",
  "Password" : "bar"
};
client.addSoapHeader(soapHeader);

【讨论】:

  • addSoapHeader 在标头中创建新标签,但我想创建一个不同的标头(“env:Header”,而不是“soap:Header”)。知道该怎么做吗?
【解决方案2】:

通读node-soap 的自述文件,如果您想要做的不是WS-Security(我不知道,因为我远离SOAP远离),那么您就是将不得不向作者提出问题,因为我看不到根据文档设置自定义标题的方法。

如果它WS-Security,则按照说明on this part of the README

【讨论】:

  • David 看来你是对的,我已经对其进行了定制,并将很快推出。但我想说的是 node.js 根本不能很好地与肥皂搭配。
  • 我会说这可能是因为 SOAP 是一个噩梦般的标准,它源于“企业”开发中的所有错误和邪恶。
【解决方案3】:

根据documentation,对于聚合的HTTP Headers,可以放headers,示例代码:

soap.createClient(url, 

    function (err, client) {
      if(err){
          console.log(err);
      } else{
          console.log(client.describe())
          var soapHeaders = {
              'Channel':'PB',
              'RqUID':'1987'
          }
          client.addHttpHeader('<nameH1>', 'valueH1');
          client.addHttpHeader('<nameH2>', 'valueH2');
//then, continue calling the soap operation 

}

【讨论】:

  • 谢谢你——我的意思是,我觉得这违背了使用 SOAP 的全部目的,但我的客户设置并不完美——谁是完美的?这感觉像是几种不同技术堆栈的混合......但是,嘿,数据流。
【解决方案4】:
soap = require('soap')
parseString = require('xml2js').parseString

soap.createClient('https://api.bingads.microsoft.com/Api/Advertiser/AdIntelligence/v9/AdIntelligenceService.svc?wsdl', function(err, client) {
  var soapHeader = {
    'AuthenticationToken': process.argv[2],
    'DeveloperToken': process.argv[3],
    'CustomerId': process.argv[4],
    'CustomerAccountId': process.argv[5]
  };
  client.addSoapHeader(soapHeader);
  client.SuggestKeywordsFromExistingKeywords({Keywords: ["Hello world"]}, function(err, result) {
    console.log(result.body);
  });
});

这行不通。回复是无效的登录凭据。相同的凭据适用于 SOAPUI。因此,其他答案中提到的发送登录凭据的格式肯定是错误的。

【讨论】:

  • 您必须为肥皂标题中的每个值添加 'tns:AuthenticationToken': &lt;value&gt; 前缀
【解决方案5】:

这对我有用。创建客户端方法需要 wsdl_header 来检索 wsdl 定义。创建后,您需要设置基本身份验证安全性。

var url = 'your WSDL url';
var auth = "Basic " + new Buffer("username" + ":" + "password").toString("base64");

soap.createClient( url,{ wsdl_headers: {Authorization: auth} }).then(
    function(client){
        client.setSecurity(new soap.BasicAuthSecurity('rflhconfort_ws', '6syqxje9'));
        client.your_Method(args); 
    }

【讨论】:

    【解决方案6】:

    我遇到了同样的问题,我在下面解决了它,这对我有用 我使用 SOAPUI 客户端插入数据的 wsdl(查看需要哪些字段)

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:yourUrn">
    <soapenv:Header>
      <urn:AuthenticationMethod>
         <urn:userName>username</urn:userName>
         <urn:password>password</urn:password>
       </urn:AuthenticationMethod>
        </soapenv:Header>
        <soapenv:Body>
        <urn:SoapFunctionToCall>
         <urn:Field1>Text</urn:Field1>
          <urn:Field2>Text</urn:Field2>
           <urn:Field3>Text</urn:Field3>
            <urn:Field14>Text</urn:Field4>
             <urn:Field5>Text</urn:Field5>
             <urn:Field6>Text</urn:Field6>
          </urn:SoapFunctionToCall>
         </soapenv:Body>
        </soapenv:Envelope>
    

    下面是我在node中调用的方法

    function createSoapEntry(){
    let url = "your wsdl url"
    var credentials = {
        AuthenticationMethod:{
            userName: "username",
            password: "password"
        }  
    }
    let args = { 
                Field1:"Text",
                Field2:"Text",
                Field3:"Text",
                Field4:"Text",
                Field5:"Text",
                Field6:"Text"            
        }
    soap.createClient(url, function (err, client){
        client.addSoapHeader(credentials)
    
        client.SoapFunctionToCall(args, function (err, res) {
            if (err) {
                console.log("Error is ----->" + err)
            } else {
                console.log("Response is -----> " + res)
            }
        })
    })
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多