SOAP Usage
In FaasPlus, you can convert SOAP service responses into REST-friendly formats. By leveraging
fetch
to make XML-based requests and native JavaScript regex parsing, you can work with SOAP data
in a
RESTful way. This approach allows you to call SOAP services, parse the XML responses,
and return the data in JSON format without external dependencies.
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.
export async function handler(event) {
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>${event.body?.A}</intA>
<intB>${event.body?.B}</intB>
</Add>
</soap:Body>
</soap:Envelope>`;
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: body
});
const xmlResponse = await response.text();
// Parse XML response using regex (no external libraries in V8)
const match = xmlResponse.match(/<AddResult>(\d+)<\/AddResult>/);
const result = match ? Number(match[1]) : 0;
return { result };
}
body (as json)
{
"A":10,
"B":20
}
>response
{
"result": 30
}