Making SOAP requests
You can use the API Action to make REST-based API calls. If the API you're integrating with is SOAP-based, the Code Editor is the tool for you.

Example

The following is an example of how you can leverage the fetch library embedded in the Code Editor runtime to perform SOAP requests.
The input variable should contain any stringified number, like "500". We build the XML body through a template literal and substitute the input value. We then extract the response value through Regex. The output message in the chat will then contain the number as words, "five hundred" in the case of our example.
1
// We make use of the following public API
2
// https://documenter.getpostman.com/view/8854915/Szf26WHn
3
​
4
const { input } = args;
5
​
6
const url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso";
7
​
8
// The request body
9
const body = `<?xml version="1.0" encoding="utf-8"?>
10
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
11
<soap:Body>
12
<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
13
<ubiNum>${input}</ubiNum>
14
</NumberToWords>
15
</soap:Body>
16
</soap:Envelope>
17
`;
18
​
19
const headers = {
20
// Additional headers may be required
21
// dpending on your server implementation
22
"Content-Type": "text/xml; charset=utf-8",
23
};
24
​
25
const options = { method: "POST", headers, body };
26
​
27
const chatlayer = ChatlayerResponseBuilder();
28
​
29
try {
30
// Perform the request to the SOAP api
31
const response = await fetch(url, options);
32
33
// Extract the text value from the response
34
const xmlResponse = await response.text();
35
36
// Build and match the RegExp to extract data from the response
37
const regExp = new RegExp(
38
"<m:NumberToWordsResult>(.*?)</m:NumberToWordsResult>"
39
);
40
const match = xmlResponse.match(regExp);
41
if (!match) {
42
throw new Error("Could not find the given number");
43
}
44
const result = match[1];
45
chatlayer.addMessage(result);
46
} catch (ex) {
47
chatlayer.addMessage(ex.toString());
48
} finally {
49
chatlayer.send();
50
}
Copied!
​
​
​
Last modified 3mo ago
Copy link
Contents
Example