A newer version of this documentation is available.

View Latest

Function: Basic cURL GET

    March 23, 2025
    + 12

    Goal: Perform a simple cURL GET using an external REST endpoint.

    • This function basicCurlGet communicates with a public REST service.

    • Requires Eventing Storage (or metadata collection) and a "source" collection.

    • 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 either of the following (but test to make sure the service is live):

      shell
      curl -q -X GET 'https://api.frankfurter.app/latest' curl -q -X GET '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 (or to make your own service), refer to External REST via cURL GET.

    javascript
    // To run configure the settings for this Function, basicCurlGet, as follows: // // Version 7.1+ // "Function Scope" // *.* (or try bulk.data if non-privileged) // Version 7.0+ // "Listen to Location" // bulk.data.source // "Eventing Storage" // rr100.eventing.metadata // Binding(s) // 1. "binding type", "alias name...", "URL...", "misc.", // "URL alias", "exchangeRateApi", "https://api.frankfurter.app/", "no auth" // // Version 6.X // "Source Bucket" // source // "MetaData Bucket" // metadata // Binding(s) // 1. "binding type", "alias name...", "URL...", "misc.", // "URL alias", "exchangeRateApi", "https://api.frankfurter.app/", "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 could supply YYYY-MM-DD // perform the cURL request using the URL alias from 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_col[meta.id] = response.body; } } catch (e) { log("cURL request had an exception:",e) } }