Make HTTP requests from Adobe Campaign to integrate with external services and APIs.
All examples below use the π execute method of HttpClientRequest
Preparation
All URLs must be whitelisted via the Adobe Control Panel.
Walkthrough
Simple HTTP GET
var url = "https://jsonplaceholder.typicode.com/users/1";
var req = new HttpClientRequest(url);
req.execute();
var resp = req.response;
var user = JSON.parse(resp.body);
logInfo(user.name); // "Leanne Graham"HTTP GET with params ?var1=1&var2=2
var base = "https://jsonplaceholder.typicode.com/posts";
var params = {
postId: 1,
userId: "1",
};
// then convert them to a URL format var1=1&var2=2
var query = Object.keys(params)
.map(function (k) {
return k + "=" + params[k];
})
.join("&");
var url = base + "?" + query; // https://jsonplaceholder.typicode.com/posts?userId=1&id=1
var req = new HttpClientRequest(url);
req.execute();
var resp = req.response;
var posts = JSON.parse(resp.body);
logInfo(posts[0].id); // 1HTTP POST as form
var req = new HttpClientRequest("https://url.com/page-with-form");
req.method = "POST";
req.body = "[email protected]&var2=4324f83488dwq84dw4e84dw4dew84";
req.header["Content-Type"] = "application/x-www-form-urlencoded";
req.execute();
var response = req.response;
if (response.code != 200) {
logError("Server returned not HTTP 200: HTTP", response.code, response.body);
}
var body = "" + response.body; // convert to string
logInfo("HTTP response body", body);Validation & troubleshooting
If you donβt whitelist your target URLs youβll get a JS Exception:
02/15/2019 11:37:29 AM Workflow 'WKF471' is being run
02/15/2019 11:37:29 AM js SCR-160012 JavaScript: error while evaluating script 'WKF471/js'.
02/15/2019 11:37:29 AM js JST-310026 The URL 'https://jsonplaceholder.typicode.com/users/1' that the script is trying to access is not part of the authorized addresses in urlPermission (serverconf.xml)
Going further
HTTP Basic Authentication
var http = new HttpClientRequest("http://www.somewhere.com/login");
var buffer = new MemoryBuffer();
buffer.fromString("MyLogin" + ":" + "MyPassword", "iso-8859-1");
http.header["Authorization"] = "Basic " + buffer.toBase64();
http.header["Content-Type"] = "text/xml; charset=utf-8";
http.method = "SEARCH";
http.body = "<xml>any content</xml>";
http.execute();Asynchronous HTTP query
Use the π execute method of HttpClientRequest with async:
// get records from previous transition
var query = NLWS.xtkQueryDef.create({ queryDef: {} }); // @see /2018/08/use-querydef-the-database-toolkit-in-adobe-campaign#select-raw-data-from-workflow-transition
var records = query.ExecuteQuery().getElements();
var successRequests = []; // used to store successfuly HttpClientRequest for "HttpClientRequest.wait()"
var failedRequests = []; // used to store failed HttpClientRequest, if a retry is needed
// function called after each async API call
var onComplete = function (req, context, status) {
// compute the response: get data from var xmlDoc = DOMDocument.fromXMLString(String(resp.body)); then sqlExec(...)
// call next
sendNext(context);
};
// for each record
function sendNext(context) {
// Repeat until the first execute() passes
while (records.length != 0) {
// get and remove first element
var record = records.shift();
// create request
var req = new HttpClientRequest(endpoint);
req.complete = onComplete; // assign complete callback
var hasProxy = false;
var async = true;
var timeout = 2000;
try {
req.execute(hasProxy, async, timeout, context);
successRequests.push(req); // store successful requests
return; // exit sendNext function because will be called by onComplete
} catch (error) {
logWarning("Error:", error);
failedRequests.push(req); // store failed requests
continue; // continue because not called by onComplete
}
}
}
// Start 3 requests at the same time
sendNext({ id: 1 }); // use any variable, such as an id, to debug
sendNext({ id: 2 });
sendNext({ id: 3 });
// Wait until all requests are completed
HttpClientRequest.wait(successRequests);See this tutorial to install JS library such as underscore or Moment JS in Adobe Campaign.