You can use the global $http.send(config)
helper to send HTTP requests to external services.
This could be used for example to retrieve data from external data sources, to make custom requests to a payment
provider API, etc.
Below is a list with all currently supported configurations and their defaults.
// throws on timeout or network connectivity error
const res = $http.send({
url: "",
method: "GET",
body: "", // eg. JSON.stringify({"test": 123})
headers: {"content-type": "application/json"},
timeout: 120, // in seconds
})
console.log(res.headers) // the response headers (eg. res.headers['X-Custom'][0])
console.log(res.cookies) // the response cookies (eg. res.cookies.sessionId.value)
console.log(res.statusCode) // the response HTTP status code
console.log(res.raw) // the response body as plain text
console.log(res.json) // the response body as parsed json array or map
Here is an example that will enrich a single book record with some data based on its ISBN details from openlibrary.org.
onRecordBeforeCreateRequest((e) => {
const isbn = e.record.get("isbn");
// try to find the published date
try {
const res = $http.send({
url: "https://openlibrary.org/isbn/" + isbn + ".json",
})
if (res.statusCode == 200) {
e.record.set("published", res.json.publish_date)
}
} catch (err) {
console.log("request failed", err);
}
}, "books")
Some implementation caveats:
- All requests are processed synchroniously.
multipart/form-data
requests are currently not supported (this may change in the future depending on the users demand).