Google Apps Script:
Sending an HTTP request

How to:

In Google Apps Script, the primary way to send an HTTP request is by using the UrlFetchApp service. This service provides methods to make HTTP GET and POST requests. Here’s a simple example of making a GET request to retrieve JSON data:

function fetchJsonData() {
  var url = 'https://api.example.com/data';
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  Logger.log(data);
}

For a POST request, which is commonly used to send data to a server, you need to include more details in the options parameter:

function postExample() {
  var url = 'https://api.example.com/post';
  var payload = {
    key1: 'value1',
    key2: 'value2'
  };
  
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    // Convert the JavaScript object to a JSON string
    'payload' : JSON.stringify(payload)
  };
  
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

These snippets show basic GET and POST request implementations. The output will depend on the API response and can be viewed in Google Apps Script’s Logger.

Deep Dive

Google Apps Script’s UrlFetchApp service has evolved significantly since its inception, offering more nuanced control over HTTP requests with features like setting headers, payload, and handling multipart/form-data for file uploads. While it provides a straightforward means to integrate external web services, developers coming from more robust backend languages may find its functionality somewhat limiting compared to libraries like Python’s requests or JavaScript’s fetch API in Node.js.

One notable limitation is the execution time limit for Google Apps Script, which affects long-running requests. Additionally, while UrlFetchApp covers a wide range of use cases, more complex scenarios involving OAuth authentication or handling very large payloads may require creative solutions or leveraging additional Google Cloud resources.

Nonetheless, for most integrations that Google Workspace developers encounter—ranging from automating data retrieval to posting updates to external services—UrlFetchApp provides a potent, accessible tool. Its integration into Google Apps Script means there’s no need for external libraries or complex setup, making HTTP requests relatively straightforward to execute within the constraints of Google Apps Script. As the landscape of web APIs continues to expand, UrlFetchApp remains a critical bridge for Google Apps Script programs to interact with the world beyond Google’s ecosystem.