Nasir Khan
Nasir Khan

Reputation: 813

Call SOAP API from nodejs and use(json) response data

I call a SOAP API from POSTMAN it is working fine. Now I want to use it with Node.js(nextjs). This is how i am calling API and what I am getting. enter image description here

How i will call this and get a json response? Now i am calling like this-

const xml =
  "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><HourlyTransactions xmlns='https://secure.myterminals.com/ConfigStatusSyncService'><StartTime>2020-12-17 00:00</StartTime><EndTime>2020-12-18 00:00</EndTime><Login></Login><Password></Password></HourlyTransactions></soap:Body></soap:Envelope>";
  
  let res = await fetch(
    "https://secure.myterminals.com/ConfigStatusSyncService/DataQuery.asmx",
    {
      method: "POST",
      headers: { "Content-Type": "text/xml" },
      body: xml,
    }
    );
    console.log(res);

Upvotes: 1

Views: 5168

Answers (2)

Reza Attar
Reza Attar

Reputation: 618

I would rather use easy-soap-request package. Here's a working example:

const soapRequest = require('easy-soap-request');
const url = 'https://secure.myterminals.com/ConfigStatusSyncService/DataQuery.asmx';
const sampleHeaders = {
    'Content-Type': 'text/xml',
    // 'soapAction': '' //FILL_HERE If Needed
};

const xmlSoapEnvelope = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><HourlyTransactions xmlns='https://secure.myterminals.com/ConfigStatusSyncService'><StartTime>2020-12-17 00:00</StartTime><EndTime>2020-12-18 00:00</EndTime><Login></Login><Password></Password></HourlyTransactions></soap:Body></soap:Envelope>";
(async () => {
    try {
        const {
            response
        } = await soapRequest({
            url: url,
            headers: sampleHeaders,
            xml: xmlSoapEnvelope,
            timeout: 1000
        });
        const {
            headers,
            body,
            statusCode
        } = response;
        console.log('soap headers:', headers);
        console.log('soap body:', body);
    }
    catch(e) {
        console.log("Error Due to: ", e)
    }
})()

Upvotes: 1

Linuxuser07
Linuxuser07

Reputation: 46

I hope this can help I would use the easey-soap-request and here is an example.

import soapRequest from "easy-soap-request";
const url = "https://my-soap-server";
const sampleHeaders = {
  "Content-Type": "text/xml;charset=UTF-8",
  SOAPAction: "https://my-soap-action/something"
};
const xml = `<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<HourlyTransactions xmlns='https://secure.myterminals.com/ConfigStatusSyncService'>
<StartTime>2020-12-17 00:00</StartTime><EndTime>2020-12-18 00:00</EndTime><Login></Login><Password></Password></HourlyTransactions>
</soap:Body>
</soap:Envelope>`;
async function makeRequest() {
  const { response } = await soapRequest({
    url: url,
    headers: sampleHeaders,
    xml: xml,
    timeout: 1000
  });
  const { headers, body, statusCode } = response;
  console.log(headers);
  console.log(body);
  console.log(statusCode);
  document.body.innerHTML = body;
}
makeRequest();

This is the example I found HERE

Upvotes: 1

Related Questions