SOAP Usage

In FaasPlus, you can convert SOAP service responses into REST-friendly formats. By leveraging fetch to make XML-based requests and xml2js to parse the XML responses, you can work with SOAP data in a RESTful way. This approach allows you to call SOAP services, parse the XML responses into JavaScript objects, and return the data in JSON format.


Example: Adding Two Integers and Returning the Result as JSON

In this example, we’ll use a public SOAP service, http://www.dneonline.com/calculator.asmx, which takes two parameters, intA and intB, and returns their sum.

const handleRequest = async (fetch, xml2js, req) => {
    const url = 'http://www.dneonline.com/calculator.asmx';
    const headers = {
        'Content-Type': 'text/xml; charset=utf-8',
        'SOAPAction': 'http://tempuri.org/Add'
    };

    const body = `<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>
            <Add xmlns="http://tempuri.org/">
                <intA>${req.body.A}</intA>
                <intB>${req.body.B}</intB>
            </Add>
        </soap:Body>
    </soap:Envelope>`;

    const response = await fetch(url, {
        method: 'POST',
        headers: headers,
        body: body
    });

    const xmlResponse = await response.text();
    const parser = new xml2js.Parser({ explicitArray: false });
    const jsonResult = await parser.parseStringPromise(xmlResponse);

    const result = Number(jsonResult["soap:Envelope"]["soap:Body"]["AddResponse"]["AddResult"]);
    return { result };           
};
body (as json)
{
    "A":10,
    "B":20
}
>response
{
    "result": 30
}