Internet Access
Declaration
imports:
my_http_client:
http_client:
# Declare the website you want to access
baseUrl: https://example.com
# Optionally add headers
headers:
HEADER1: VALUEJavaScript API
Methods
get(path) -> JsHttpResponse: make a GET request to{baseUrl}/{path}delete(path) -> JsHttpResponse: make a DELETE request to{baseUrl}/{path}head(path) -> JsHttpResponse: make a HEAD request to{baseUrl}/{path}options(path) -> JsHttpResponse: make a OPTIONS request to{baseUrl}/{path}post(path, body): make a POST request to{baseUrl}/{path}body(HttpBody): Request body. Can be either a string or JSON object
put(path, body): make a PUT request to{baseUrl}/{path}body(HttpBody): Request body. Can be either a string or JSON object
patch(path, body): make a PATCH request to{baseUrl}/{path}body(HttpBody): Request body. Can be either a string or JSON object
setOauth(credentials): see oauth for usage.withOptions(opts): returns a new client object with the selected options. Good for setting HTTP headers.opts(HttpOptions): options object
Objects
JsHttpResponsestatusCode(int): Status code for the HTTP responseheaders(object): Headers returned by the HTTP responsebody(HttpBody): Response Body
HttpBodystring(string): The HTTP response body as a stringjson(object): The HTTP body marshalled to JSON. This will be null if the response is not valid JSON.
HttpOptionsheaders(object): HTTP headers to add to the clientmergeHeaders(bool): Set to false to ignore the headers on the parent client.
Example
var response = my_http_client.get("/some/api");
console.log(response.statusCode);
console.log(response.body.json);
console.log(response.body.string);
var withCreds = my_http_client.withOptions({headers: {"Authorization": "Token 1234"}});
withCreds.post("/some/api", {json:{hello: "world"}});