Skip to content

web.get

Sends a HTTP GET request to the specified URL.

js
web.get(url, headers, base64Body)

Parameters

  • url (string): The URL to send the GET request to.
  • headers (object): An object containing custom headers to include in the request. The keys represent header names, and the values represent the corresponding header values. (optional)
  • base64Body (boolean): A boolean flag indicating whether the response body should be returned as a base64-encoded string. Default value is false. (optional)

Returns

  • response (object): An object representing the HTTP response containing the following properties:
    • status (number): The HTTP status code of the response.
    • headers (array): An array of header objects, where each object has one key/value pair representing a header.
    • body (string): The response body as a string or as a base64-encoded string if the content type is binary.
    • error (string): An error message if an exception occurred while sending the request.

Examples

js
var 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
var 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);
}