Skip to content

web.get ​

Sends an HTTP GET request to a URL.

js
web.get(url, headers, base64Body)

Parameters ​

ParameterTypeDescription
urlstringThe URL to send the request to.
headersobjectOptional. The request headers, keyed by header name.
base64BodybooleanOptional. true to always return the response body base64-encoded. Defaults to false.

Returns ​

object

The response:

  • status (number): the HTTP status code, or 0 when no response was received.
  • headers (array): the response headers, each an object with one key/value pair.
  • body (string): the response body.
  • error (string): why the request failed, when no response was received.

Binary responses

Common binary types, such as images, PDFs and audio, are detected and returned base64-encoded. To save a file, use web.download.

Status and errors

A 4xx or 5xx status means the server received the request and returned an error. See HTTP status codes.

error is only set when the request fails before a response comes back. status is then usually 0. This can happen when:

  • the URL is invalid or malformed.
  • the connection or response times out.
  • the network fails, such as a lost connection, a DNS problem, an SSL validation error or a blocked request.
  • the server drops or rejects the request without responding.

Examples ​

js
const response = web.get("https://api.example.com/data", { "Authorization": "Bearer your_token" });
console.log(response.status);    // 200
console.log(response.headers);   // Array of headers
console.log(response.body);      // Response body as a string or base64-encoded string
js
const response = web.get("https://api.example.com/data");

// Check if the response status code is in the success range (between 200 and 299, inclusive).
if (response.status >= 200 && response.status < 300) {
    console.log("Request succeeded.");
    console.log(response.body);
} else if (response.error) {
    // An error occurred while sending the request (e.g., network error, timeout, etc.).
    console.log("Error occurred while sending the request: " + response.error);
} else {
    // The request failed with a non-success status code.
    console.log("Request failed with status code: " + response.status);
    console.log(response.body);
}