Home > Software design >  How to consume SOAP web services using Node.js
How to consume SOAP web services using Node.js

Time:12-23

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.

CodePudding user response:

This is sample calculator WS example using node.js with 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.

  • Related