如何在节点soap中添加“namespace”以请求

时间:2022-04-11 20:16:14

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.

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

});

}

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.

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

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

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

1 个解决方案

#1


8  

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,

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

I first create my wsdlOptions object.

我首先创建我的wsdlOptions对象。

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客户端时使用它,

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

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

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.

现在,webserver接受了它。

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

请记住,即使tns在root中定义,它也不会自动添加到请求体中。此外,如果您尝试再次通过tns在wsdlOptions中覆盖它,它仍然无法正常工作。您必须使用不同的值作为命名空间,如myns

#1


8  

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,

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

I first create my wsdlOptions object.

我首先创建我的wsdlOptions对象。

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客户端时使用它,

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

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

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.

现在,webserver接受了它。

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

请记住,即使tns在root中定义,它也不会自动添加到请求体中。此外,如果您尝试再次通过tns在wsdlOptions中覆盖它,它仍然无法正常工作。您必须使用不同的值作为命名空间,如myns