Function: Basic cURL POST
Goal: Perform a simple cURL POST using an external REST endpoint.
-
This function basicCurlPost communicates with a public REST echo 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://postman-echo.com/post' -d '{ "myboolean": true }'
-
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. curlEchoApi
// 3. https://postman-echo.com/
// 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 POST request we see a mutation on the above KEY
var request = {
path: 'post', // can also do 'get' in this API
body: {
"myboolean": true
}
};
// perform the cURL reques using the URL alias form the settings
var response = curl('POST', curlEchoApi, request);
if (response.status != 200 && response.status != 302) {
log("cURL POST failed response.status:",response.status)
} else {
log("cURL POST success, response.body:",response.body)
// optional write to a bucket - requires a binding alias in settings
// note the response.body.json is the echo back of request.body
// 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:54:05.089-07:00 [INFO] "cURL POST success, response.body:"
{
"args": {},
"data": {
"myboolean": true
},
"files": {},
"form": {},
"headers": {
"x-forwarded-proto": "https",
"x-forwarded-port": "443",
"host": "postman-echo.com",
"x-amzn-trace-id": "Root=1-5f2c43bd-db99a094f0b6b4e0a96a8e34",
"content-length": "18",
"user-agent": "libcurl/7.66.0-DEV couchbase/evt-6.6.0-7897-ee (eventing)",
"accept": "*/*",
"accept-encoding": "deflate, gzip",
"content-type": "application/json"
},
"json": {
"myboolean": true
},
"url": "https://postman-echo.com/post"
}