27

I need to make request to SOAP endpoint using axios in my React application. Hence I need to pass xml data in request and receive xml data in response.

I have used the axios post with json data but how do I use the same for xml? PFB the code I am using for the same, but it does not work.

JSON post request:

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

Please share if you have any experience with this, TIA.

Peter
  • 10,492
  • 21
  • 82
  • 132

3 Answers3

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

This code help to make soap request

tevemadar
  • 12,389
  • 3
  • 21
  • 49
Anuragh KP
  • 748
  • 11
  • 21
  • 1
    good solutions and best answer. Thanks for answer here in stackoverflow – AMIC MING Jan 16 '20 at 03:05
  • `{headers: {'Content-Type': 'text/xml' , 'SoapAction': 'blahblah'} }` Soap service may expect the soap action name in the header. You can get it from the WSDL . I got an issue with Soap action not found. – kbvishnu May 24 '23 at 15:56
  • Its depends on how your server is handle the request you may need some extra header – Anuragh KP May 24 '23 at 16:28
14

I used the answer of @Anuragh KP but with a 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)
  })
Christian Saiki
  • 1,568
  • 15
  • 22
0

It is not using axios but I think it worth mentioning (it also rather new - 4 month at the time of answering) and solved my problem of calling SOAP from NodeJS: https://www.npmjs.com/package/soap

from the owners sample code:

  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

Qua285
  • 137
  • 1
  • 3
  • 12