首页 > 解决方案 > 如何在nodejs中向XML-WSDL中的服务发送请求

问题描述

我在 Web 服务(WSDL)中有一个 XML。该文件包含许多服务,我想向 Nodejs 中的该服务之一发送请求。但我对 XML 和 WSDL 一无所知。我只想请求服务并向其发送一些参数并获得响应。

这个 xml 服务包含一些这样的服务:

<wsdl:operation name="bpPaymentRequest">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="bpPayRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="bpPayRequestResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>

我想向这项服务请求这样的事情:

const response = await axios.post( "https://example.com/services/pgw?bpPaymentRequest" , payReqParams)

但是,如果我对此进行测试,我知道这是错误的!我应该如何向该服务发送请求?

标签: node.jsxmlsoapaxioswsdl

解决方案


It seems that you are willing to call a SOAP web service from axios.

SOAP web services talk XML. That means they accept XML as input and return another XML as response. In contrast with Json APIs, they do not accept JSON data input.

So,

  1. You need to prepare an XML for input
  2. Send it via axios
  3. Parse the result assuming that is a valid XML

A sample code would be like this:

var xml ='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\
                            xmlns:web="http://shaparak/">\
            <soapenv:Header/>\
            <soapenv:Body>\
              <web:param>\
                <web:amount>123</web:amount>\
              </web:param>\
            </soapenv:Body>\
          </soapenv:Envelope>';

axios.post('https://example.com/services/pgw?bpPaymentRequest',
           xml,
           {headers:
             {'Content-Type': 'text/xml'}
           }).then(res=>{
             console.log(res);
           }).catch(err=>{console.log(err)});

Attention

Are you implementing a bank gateway for payments with Mellat or Shaparak? They have some implementation for Node.js.


推荐阅读