HTTP
Table of contents
The HTTP API allows scripts to send HTTP requests to web services, enabling integration between Memento Database and external systems. All HTTP operations are performed through the Http
object, which is accessed via the global http()
function.
HTTP requests must be executed asynchronously in the last Phase of an Event
The library must have the “Network” permission enabled
Global functions
http()
Global function that returns an Http object for making HTTP requests.
Returns
A new Http object instance for making HTTP requests
Example
var httpClient = http();
log("Created new HTTP client");
Http Object
The Http object provides methods for making HTTP requests and managing request headers. It is obtained through the global http()
function.
get(url)
Executes an HTTP GET request to the specified URL.
Parameters
Parameter | Type | Description |
---|---|---|
url | String | The complete URL starting with http:// or https:// |
Returns
Type | Description |
---|---|
HttpResult | Object containing the response from the HTTP request |
Example
var result = http().get("http://api.fixer.io/latest?base=USD");
log("Response code: " + result.code);
log("Response body: " + result.body);
post(url, body)
Executes an HTTP POST request to the specified URL with the given body content.
Parameters
Parameter | Type | Description |
---|---|---|
url | String | The complete URL starting with http:// or https:// |
body | String | The content to send in the POST request body |
Returns
Type | Description |
---|---|
HttpResult | Object containing the response from the HTTP request |
Example
var result = http().post("http://httpbin.org/post", "Hello World");
log("POST response code: " + result.code);
headers(info)
Sets HTTP headers for subsequent requests.
Parameters
Parameter | Type | Description |
---|---|---|
info | Object | Key-value pairs of header names and values |
Returns
Type | Description |
---|---|
Http | The Http object (for method chaining) |
Example
var httpClient = http();
httpClient.headers({"User-Agent": "Memento Script", "Accept": "application/json"});
var result = httpClient.get("http://httpbin.org/get");
log("Request with custom headers completed: " + result.code);
HttpResult Object
The HttpResult object contains the response from an HTTP request.
Properties
Property | Type | Description |
---|---|---|
body | String | The response body as text |
code | Number | The HTTP status code of the response (e.g., 200 for success) |
header(tag)
Retrieves a specific header from the response.
Parameters
Parameter | Type | Description |
---|---|---|
tag | String | The name of the header to retrieve |
Returns
Type | Description |
---|---|
String | The value of the specified header |
Example
var result = http().get("http://httpbin.org/get");
var etag = result.header("etag");
log("Response ETag: " + etag);
Examples
// Get current exchange rate from API
var result = http().get("https://data.fixer.io/api/latest?access_key=[API Key]");
// Parse the JSON response
var usdToEur = JSON.parse(result.body)["rates"]["EUR"];
log("Current USD to EUR rate: " + usdToEur);
// Update entry with converted price
var usdPrice = entry().field("PriceUSD");
var eurPrice = usdPrice * usdToEur;
entry().set("PriceEUR", eurPrice);
log("Converted " + usdPrice + " USD to " + eurPrice + " EUR");