1

I have been searching information or examples about how to make requests to a SOAP web service, but the information that I have found did not help me.

I have read about strong-soap and soap, but I did not get it.

1 Answers1

2

This is sample calculator WS example using node.js with axios and jsdom

Overview http://www.dneonline.com/calculator.asmx

WSDL http://www.dneonline.com/calculator.asmx?WSDL

enter image description here

Addition and Subtraction Examples with this values

100 + 200 = 300

100 - 200 = -100

Code. save it as SOAP-calc.js

const axios = require("axios");
const jsdom = require("jsdom");

// https://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript
function formatXml (xml, tab) { // tab = optional indent value, default is tab (\t)
    let formatted = '', indent= '';
    tab = tab || '\t';
    xml.split(/>\s*</).forEach(function(node) {
        if (node.match( /^\/\w/ )) indent = indent.substring(tab.length); // decrease indent by one 'tab'
        formatted += indent + '<' + node + '>\r\n';
        if (node.match( /^<?\w[^>]*[^\/]$/ )) indent += tab;              // increase indent
    });
    return formatted.substring(1, formatted.length-3);
}

function getPayload(operation, a, b) {
    let payload = ''
    switch(operation) {
        default:
        case 'add':
            payload = `<?xml version="1.0" encoding="utf-8"?>
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Body>
                    <Add xmlns="http://tempuri.org/">
                        <intA>${a}</intA>
                        <intB>${b}</intB>
                    </Add>
                </soap:Body>
            </soap:Envelope>`            
        break;
        case 'minus':
            payload = `<?xml version="1.0" encoding="utf-8"?>
            <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Body>
                    <Subtract xmlns="http://tempuri.org/">
                        <intA>${a}</intA>
                        <intB>${b}</intB>
                    </Subtract>
                </soap:Body>
            </soap:Envelope>`            
        break;
    }
    return payload;
}
const calculate = async (operation, a, b) => {
    const url = 'http://www.dneonline.com/calculator.asmx'
    const payload = getPayload(operation, a, b)
    try {
        const response = await axios.post(
            url,
            payload,
            {
                headers: {
                    'Content-Type': 'text/xml; charset=utf-8'
                }
            }
        );
        return Promise.resolve(response.data);
    } catch (error) {
        console.log(error);
        return Promise.reject(error);
    }
};

// call SOAP Calculator service to 'http://www.dneonline.com/calculator.asmx'
// node soap-calc.js add
// node soap-calc.js minus
calculate(operation = (process.argv[2] == null)? 'add' : process.argv[2], a = 100, b = 200)
    .then(result => {
        // display response of whole XML
        console.log(formatXml(result));

        // extract result from XML
        const dom = new jsdom.JSDOM(result);
        switch(operation) {
            case 'add':
                console.log(`${a} + ${b} = ` + dom.window.document.querySelector("AddResult").textContent); // 300
                break;
            case 'minus':
                console.log(`${a} - ${b} = ` + dom.window.document.querySelector("SubtractResult").textContent); // -100
                break;
        }
    })
    .catch(error => {
        console.error(error);
    });

How to run it

node SOAP-calc.js add

OR

node SOAP-calc.js minus

Addition Result

$ node SOAP-calc.js add
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
.org/2001/XMLSchema">
        <soap:Body>
                <AddResponse xmlns="http://tempuri.org/">
                        <AddResult>300</AddResult>
                </AddResponse>
        </soap:Body>
</soap:Envelope>
100 + 200 = 300

enter image description here

Subtraction Result

$ node SOAP-calc.js minus
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
.org/2001/XMLSchema">
        <soap:Body>
                <SubtractResponse xmlns="http://tempuri.org/">
                        <SubtractResult>-100</SubtractResult>
                </SubtractResponse>
        </soap:Body>
</soap:Envelope>
100 - 200 = -100

enter image description here

I hope to you can easily add a Divide and Multiply operation


I found more easy way to consume SOAP service if not necessary low level XML handling.

soap-npm node.js library make life easier.

Demo for 4 operation (Add, Divide, Multiply and Subtract) service call

var soap = require('soap');

const url = 'http://www.dneonline.com/calculator.asmx?WSDL';

const args = {
    intA: 9,
    intB: 3
};

// 9 + 3 = 12
soap.createClient(url, {}, (err, client) => {
    client.Add(args, (err, result) => {
        console.log(result);
    });
});

// 9 - 3 = 6
soap.createClient(url, {}, (err, client) => {
    client.Subtract(args, (err, result) => {
        console.log(result);
    });
});


// 9 / 3 = 3
soap.createClient(url, {}, (err, client) => {
    client.Divide(args, (err, result) => {
        console.log(result);
    });
});

// 9 * 3 = 27
soap.createClient(url, {}, (err, client) => {
    client.Multiply(args, (err, result) => {
        console.log(result);
    });
});

Result

$ node simple-calc.js
{ SubtractResult: 6 }
{ AddResult: 12 }
{ DivideResult: 3 }
{ MultiplyResult: 27 }

How to know the input parameter intA and intB?

SoapUI automatically pickups it when you make a new project with WSDL url.

detail instruction in here

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14