How can I use SOAP web services from CloudCode?

How can I use SOAP web services from CloudCode

I recommend using the strong-soap package. Below are the steps to follow:

  1. Add the strong-soap package to your CC task's dependencies as per "Option A: Direct Approach" in the CloudCode Dependencies docs
  2. Require the package in your CC task with let soap = require('strong-soap').soap;
  3. Implement a getSoapClient helper method as follows:
async function getSoapClient(soap, url, options) {
    return new Promise((resolve, reject) => {
        soap.createClient(url, options, function(err, client) {
            if (err) {
                reject(err);
            }
            resolve(client);  
        });
    });
}
  1. Instantiate a SOAP client const client = await getSoapClient(soap, myWsdlUrl, options);
  2. Call your SOAP method as per the strong-soap docs
  3. (Optional) if your SOAP API is using XML, you may want to transform the XML response into JSON using something like xml2js

Here is some example code using a public SOAP endpoint:

let soap = require('strong-soap').soap;

export async function run() {
    // Your code here
    const wsdlUrl = "http://www.dneonline.com/calculator.asmx?WSDL";
    let options = {};

    const client = await getSoapClient(soap, wsdlUrl, options);

    let value = await client.Add({intA: '3', intB: '2'});

    console.log('result of add = ', value);
}


async function getSoapClient(soap, url, options) {
    return new Promise((resolve, reject) => {
        soap.createClient(url, options, function(err, client) {
            if (err) {
                reject(err);
            }
            resolve(client);  
        });
    });
}
2 Likes

Here is some example code using a public SOAP endpoint:```