When working with APIs, sending HTTP requests is an essential part of the process. While the curl command-line tool is widely used for this task, Google Apps Script offers an easy way to send HTTP requests using its UrlFetchApp service. In this article, we’ll explore how to perform various types of HTTP requests using Google Apps Script, with examples that mimic curl functionality.

What is Google Apps Script?

Google Apps Script is a scripting platform based on JavaScript that allows you to automate, integrate, and extend Google Workspace applications like Sheets, Docs, and Gmail. One of its powerful features is the UrlFetchApp service, which allows you to send HTTP requests directly from your script. This is equivalent to using curl in a terminal.

Why Use Google Apps Script for HTTP Requests?

Google Apps Script is a great choice for making HTTP requests for the following reasons:

  • Integration with Google Apps: You can easily integrate HTTP requests with Google Workspace services (e.g., Google Sheets, Gmail).
  • Automated Workflows: Automate data fetching, processing, and integration from third-party APIs.
  • Ease of Use: Google Apps Script is a JavaScript-based platform, so it is simple to write and maintain HTTP requests.

Sending HTTP Requests with UrlFetchApp

In Google Apps Script, the UrlFetchApp service allows you to send HTTP requests to external servers, retrieve data, and handle responses. It’s commonly used to interact with REST APIs.

1. Basic GET Request

The most straightforward way to send a request is with a GET request. This is similar to executing:

curl https://api.example.com
Google Apps Script Example:
function makeGetRequest() {
  var url = 'https://api.example.com';  // The URL to send the GET request to
  var response = UrlFetchApp.fetch(url); // Send the GET request
  Logger.log(response.getContentText());  // Log the response content
}

In the example above:

  • UrlFetchApp.fetch(url) sends a simple GET request to the specified URL.
  • The getContentText() method retrieves the response body as a string.
  • The Logger.log() method is used to log the response content for debugging purposes.

2. POST Request with JSON Payload

When sending data to a server, a POST request is typically used. This is similar to running:

curl -X POST -d '{"key":"value"}' https://api.example.com
Google Apps Script Example:
function makePostRequest() {
  var url = 'https://api.example.com'; // The URL to send the POST request to
  var payload = JSON.stringify({
    key: 'value'  // The data to send in the POST body
  });

  var options = {
    method: 'post',  // Specify the HTTP method
    contentType: 'application/json',  // Set content type as JSON
    payload: payload  // Send the data
  };

  var response = UrlFetchApp.fetch(url, options);  // Send the POST request
  Logger.log(response.getContentText());  // Log the response content
}

Here:

  • The method: 'post' option specifies the HTTP POST method.
  • The contentType: 'application/json' header tells the server that the payload is in JSON format.
  • payload: payload sends the JSON data in the body of the request.

3. Adding Headers (Authentication)

Sometimes, you need to send headers, such as an authentication token, with your HTTP request. This is similar to running:

curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com
Google Apps Script Example:
function makeRequestWithHeaders() {
  var url = 'https://api.example.com';
  var headers = {
    'Authorization': 'Bearer YOUR_TOKEN',  // Include an Authorization header
    'Content-Type': 'application/json'     // Set content type as JSON
  };

  var options = {
    method: 'get',  // Specify the HTTP method
    headers: headers  // Include the headers
  };

  var response = UrlFetchApp.fetch(url, options);  // Send the request with headers
  Logger.log(response.getContentText());  // Log the response content
}

In this example:

  • The headers object contains custom headers like Authorization and Content-Type.
  • These headers are passed into the UrlFetchApp.fetch() method, allowing you to send a request with additional information (e.g., API tokens or custom headers).

4. Sending Query Parameters (GET Request with Params)

To send data in the URL itself (commonly used with GET requests), you can append query parameters. This is similar to:

curl "https://api.example.com?key=value"
Google Apps Script Example:
function makeGetRequestWithParams() {
  var url = 'https://api.example.com?key=value';  // URL with query parameters
  var response = UrlFetchApp.fetch(url);  // Send the GET request
  Logger.log(response.getContentText());  // Log the response content
}

Here, the query parameters key=value are included directly in the URL. The UrlFetchApp.fetch() method sends the GET request, and the server processes the query parameters.

Handling Responses

The response from an HTTP request in Google Apps Script can be accessed using various methods from the HTTPResponse object returned by UrlFetchApp.fetch(). For example:

  • response.getContentText() retrieves the response body as a string.
  • response.getResponseCode() returns the HTTP status code.
  • response.getHeaders() returns the response headers as an object.

Error Handling

It’s important to handle errors and exceptions when making HTTP requests. Google Apps Script provides a way to catch and handle exceptions using try...catch blocks.

Example:

function makeRequestWithErrorHandling() {
  try {
    var url = 'https://api.example.com';
    var response = UrlFetchApp.fetch(url);  // Send the GET request
    Logger.log(response.getContentText());  // Log the response content
  } catch (e) {
    Logger.log('Error: ' + e.message);  // Log the error message
  }
}

This way, if the request fails or the server responds with an error, the script will catch the exception and log it for debugging.


Google Apps Script's UrlFetchApp service provides a powerful and easy way to send HTTP requests, allowing you to interact with REST APIs and external services directly from Google Sheets, Docs, or other Google Workspace apps. Whether you're sending GET requests, POST requests with JSON payloads, or adding custom headers for authentication, the process is simple and efficient.

By using UrlFetchApp in Google Apps Script, you can automate workflows, fetch data from external sources, and integrate with a wide range of third-party services.