使用 axios 向 SOAP 端点发出请求

Make request to SOAP endpoint using axios

我需要在我的 React 应用程序中使用 axios 向 SOAP 端点发出请求。因此我需要在请求中传递 xml 数据并在响应中接收 xml 数据。

我已经将 axios post 与 json 数据一起使用,但如何将其用于 xml? PFB 我正在使用的代码相同,但它不起作用。

JSON post 请求:

var xmlData = <note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

var config = {
  headers: {'Content-Type': 'text/xml'}
};

axios.post('/save', xmlData, config);

TIA,如果您对此有任何经验,请分享。

let xmls='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\
                            xmlns:web="http://www.webserviceX.NET/">\
            <soapenv:Header/>\
            <soapenv:Body>\
              <web:ConversionRate>\
                <web:FromCurrency>INR</web:FromCurrency>\
                <web:ToCurrency>USD</web:ToCurrency>\
              </web:ConversionRate>\
            </soapenv:Body>\
          </soapenv:Envelope>';

axios.post('http://www.webservicex.com/CurrencyConvertor.asmx?wsdl',
           xmls,
           {headers:
             {'Content-Type': 'text/xml'}
           }).then(res=>{
             console.log(res);
           }).catch(err=>{console.log(err)});

此代码有助于制作 soap 请求

我使用了@Anuragh KP 的答案,但使用了 SOAPAction header

axios.post('https://wscredhomosocinalparceria.facilinformatica.com.br/WCF/Soap/Emprestimo.svc?wsdl',
           xmls,
  {headers:
  {
    'Content-Type': 'text/xml',
    SOAPAction: 'http://schemas.facilinformatica.com.br/Facil.Credito.WsCred/IEmprestimo/CalcularPrevisaoDeParcelas'}
  }).then(res => {
    console.log(res)
  }).catch(err => {
    console.log(err.response.data)
  })

它没有使用 axios,但我认为它值得一提(它也相当新 - 在回答时 4 个月)并解决了我从 NodeJS 调用 SOAP 的问题:https://www.npmjs.com/package/soap

来自所有者示例代码:

  var soap = require('soap');
  var url = 'http://example.com/wsdl?wsdl';
  var args = {name: 'value'};
  soap.createClientAsync(url).then((client) => {
    return client.MyFunctionAsync(args);
  }).then((result) => {
    console.log(result);
  });

url to code quote