Function: Basic cURL GET
Goal: Perform a simple cURL GET using an external REST endpoint.
-
This function basicCurlGet communicates with a public REST service.
-
Requires a metadata bucket and a source bucket.
-
Needs a Binding of type URL Alias (as documented in the Scriptlet).
-
Will operate on any mutation of the KEY "make_curl_request::1".
-
The actual cURL request from the Eventing Function will be equivalent to:
curl -q https://api.ratesapi.io/api/latest
-
Only logs the REST response JSON payload to the Application log file.
-
For a more complete example using this public REST endpoint, refer to External REST via cURL GET.
// To run need a Binding in this Function's Settings to apublic API as follows:
// 1. URL Alias
// 2. exchangeRateApi
// 3. https://api.ratesapi.io/api/
// 4. "no auth"
function OnUpdate(doc, meta) {
// You would typically filter to mutations of interest
if (meta.id !== 'make_curl_request::1') return;
try {
// only make a cURL GET request id we see a mutation on the above KEY
var request = { path: "latest" }; // easiest API call
// perform the cURL reques using the URL alias form the settings
var response = curl('GET', exchangeRateApi, request);
if (response.status != 200 && response.status != 302) {
log("cURL GET failed response.status:",response.status)
} else {
log("cURL GET success, response.body:",response.body)
// optional write to a bucket - requires a binding alias in settings
// dst_bkt[meta.id] = response.body;
}
} catch (e) {
log("cURL request had an exception:",e)
}
}
INPUT: KEY make_curl_request::1
{
"anything": 1
}
2020-08-06T10:28:25.086-07:00 [INFO] "cURL GET success, response.body:"
{
"base": "EUR",
"rates": {
"GBP": 0.90033,
"HKD": 9.1786,
"IDR": 17304,
"ILS": 4.0309,
"DKK": 7.45,
"INR": 88.765,
"CHF": 1.0759,
"MXN": 26.615,
"CZK": 26.202,
"SGD": 1.6236,
"THB": 36.832,
"HRK": 7.468,
"MYR": 4.9604,
"NOK": 10.6538,
"CNY": 8.2325,
"BGN": 1.9558,
"PHP": 58.136,
"SEK": 10.3165,
"PLN": 4.4073,
"ZAR": 20.7655,
"CAD": 1.5748,
"ISK": 160.2,
"BRL": 6.334,
"RON": 4.836,
"NZD": 1.7828,
"TRY": 8.5853,
"JPY": 124.96,
"RUB": 86.9321,
"KRW": 1404.99,
"USD": 1.1843,
"HUF": 346.23,
"AUD": 1.6492
},
"date": "2020-08-06"
}