使用 axios 向 SOAP 端点发出请求

问题描述 投票:0回答:4

我需要在我的

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

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

JSON 发布请求:

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。

xml reactjs post soap axios
4个回答
53
投票
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)});

此代码有助于发出肥皂请求


14
投票

我使用了 @Anuragh KP 的答案,但带有 SOAPAction 标头

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

0
投票

它没有使用 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);
  });

引用代码的网址


0
投票

内容类型:application/soap+xml

如果出现此错误,则“传输级别信息与 SOAP 消息命名空间 URI 不匹配”。

© www.soinside.com 2019 - 2024. All rights reserved.