如何添加“命名空间"?在节点soap中请求

编程入门 行业动态 更新时间:2024-10-23 17:28:25
本文介绍了如何添加“命名空间"?在节点soap中请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我从 wsdl 文件创建了 Web 服务对象,并像下面这样调用它.我也打印结果.此代码不起作用.Web 服务器返回错误.因为,请求没有命名空间.

I created web service object from wsdl file and call it like in below. I also print result. this code does not work. web server returns error. because, request has not namespace.

soap.createClient(__dirname + '/wsdl/getCustomerDetails.wsdl', function(err, client) {
.
.
var params=  {"custId":"123"};
client.getCustomerDetails.getCustomerDetailsPort.getCustomerDetails(params,function(err,result){
    console.log("lastRequest:"+client.lastRequest);
    if (err != null)
        console.log("error:"+JSON.stringfy(error));

});

}

这是我在上次请求中看到的

here what I see in the last request

<soap:...  xmlns:tns="http://example/api/getCustomerDetails/V01" >
....
  <soap:Body>
     <getCustomerDetailsRequest>
         <custId>123</custId>
     </getCustomerDetailsRequest>
  </soap:Body>...

但它必须是

<soap:...  xmlns:tns="http://example/api/getCustomerDetails/V01" >
    ....
   <soap:Body>
     <tns:getCustomerDetailsRequest>
        <tns:custId>123</tns:custId>
     </tns:getCustomerDetailsRequest>
   </soap:Body>...

如您所见,soap 模块不会将 tns 命名空间添加到请求中.我尝试了 var params= {"tns:custId":"123"};,它为参数添加了命名空间,但它仍然没有为请求添加命名空间,getCustomerDetailsRequest.因此,我发现了 Unexpected element getCustomerDetailsRequest.预期为 {http://example/api/getCustomerDetails/V01}getCustomerDetailsRequest.

as you see, soap module does not add tns namespace to the request. I tried var params= {"tns:custId":"123"};, it adds namespace to the parameter, but still it does not add namespace to the request, getCustomerDetailsRequest. because of that, I get Unexpected element getCustomerDetailsRequest found. Expected {http://example/api/getCustomerDetails/V01}getCustomerDetailsRequest.

如何强制将此命名空间添加到方法本身?

how can I force to add this namespace to the method itself ?

推荐答案

我找到了如何做到这一点.我认为由于soap"模块中的错误,它在默认情况下不起作用.默认情况下,它不会向请求正文添加命名空间.soap"默认使用tns"作为命名空间.wsdlOptions 中有一个选项.它是 overrideRootElement.如果您尝试使用 tns 覆盖 overrideRootElement,它不会将 tns 添加到请求正文中.您必须在 overrideRootElement 中使用不同的命名空间.这是我的解决方案,

I found how I can do that. I think it does not work in default because of a bug in "soap" module. In default, it does not add namespace to the request body. "soap" uses "tns" in default as namespace. there is an option in wsdlOptions. it is overrideRootElement. If you try to override overrideRootElement with tns, it does not add tns to the request body. You have to use different namespace in overrideRootElement. Here my solution,

我首先创建了我的 wsdlOptions 对象.

I first create my wsdlOptions object.

var wsdlOptions = {
  "overrideRootElement": {
    "namespace": "myns",
    "xmlnsAttributes": [{
      "name": "xmlns:myns",
      "value": "http://example/api/getCustomerDetails/V01"
    }]
  }
};

然后在我创建soap客户端时使用它,

and then use it when I create soap client,

soap.createClient(__dirname + '/wsdl/getCustomerDetails.wsdl',wsdlOptions, function(err, client) {
.
.
.
}
);

它使请求主体使用 myns 作为根元素的命名空间.现在,我必须修改参数的命名空间,所以我将参数定义为

It makes request body uses myns as namespace of root element. Now, I have to modify parameters' namespace, so I defined parameters as

var params=  {"myns:custId":"123"};

现在,它创建请求为

<soap:...  xmlns:tns="http://example/api/getCustomerDetails/V01" >
    ....
   <soap:Body>
     <myns:getCustomerDetailsRequest xmlns:myns="http://example/api/getCustomerDetails/V01">
        <myns:custId>123</tns:custId>
     </myns:getCustomerDetailsRequest>
   </soap:Body>...

而且,现在网络服务器接受它.

and, now webserver accepts it.

请记住,即使 tns 是在根中定义的,它也不会自动添加到请求正文中.此外,如果您再次尝试通过 tnswsdlOptions 中覆盖它,它仍然不起作用.您必须使用不同的值作为命名空间,例如 myns

keep in mind, even tns is defined in the root, it is not added to the request body automatically. Also, if you try to override it in wsdlOptions by tns again, it still does not work. You have to use different value as namespace, like myns

这篇关于如何添加“命名空间"?在节点soap中请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 14:00:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/833412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   空间   quot   soap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!