如何添加“名称空间"请求节点肥皂 [英] How can I add "namespace" to request in node soap

查看:91
本文介绍了如何添加“名称空间"请求节点肥皂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

});

}

这是我在上一个请求中看到的内容

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

但必须是

<soap:...  xmlns:tns="http://example.com/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 found. Expected {http://example.com/api/getCustomerDetails/V01}getCustomerDetailsRequest.

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

解决方案

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

我首先创建我的wsdlOptions对象.

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

然后在创建肥皂客户端时使用它,

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

它使请求正文使用myns作为根元素的名称空间.现在,我必须修改参数的名称空间,因此我将参数定义为

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

现在,它以

的形式创建请求

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

,并且现在Web服务器接受它.

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

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.com/api/getCustomerDetails/V01" >
....
  <soap:Body>
     <getCustomerDetailsRequest>
         <custId>123</custId>
     </getCustomerDetailsRequest>
  </soap:Body>...

but it has to be

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

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.com/api/getCustomerDetails/V01}getCustomerDetailsRequest.

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

解决方案

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,

I first create my wsdlOptions object.

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

and then use it when I create soap client,

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

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"};

now, It creates request as

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

and, now webserver accepts it.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆