メインコンテンツにスキップ

http-request-app

A Fluent interface for Google Apps Script URL Fetch Service, to simplify 3rd party API integrations

AI Summary

Http Request App

A wrapper library that streamlines and enhances HTTP/REST requests in Google Apps Script.

Target Users

Intermediate GAS developers building business automations that integrate with external REST APIs.

Problems Solved

The native UrlFetchApp is verbose for headers, error handling, and maintenance when calling multiple APIs.

Tags

Main Features

1
Intuitive Builder Syntax

Chainable methods allow setting method, headers, query and payload, greatly improving readability.

2
JSON Auto-Parsing

Automatically stringifies request bodies and parses JSON responses, eliminating manual handling.

3
Retry & Pagination Utilities

Built-in helpers for exponential back-off retries and paginated data retrieval within Apps Script quotas.

Usage Examples

Introduction – Quick GET request

// After adding the library, just run this.
const response = HttpRequestApp.get('https://api.example.com/items').fetch();
Logger.log(response.getContentText()); // Inspect raw JSON

The smallest example: only the URL is needed. The library internally calls UrlFetchApp.fetch and returns the response.

Basic – POST with JSON body

const payload = {name: 'GAS', type: 'library'};
const response = HttpRequestApp.url('https://api.example.com/items')
  .method('POST')                 // Set HTTP method
  .header('Authorization', 'Bearer ' + ScriptApp.getOAuthToken())
  .json(payload)                  // Auto-stringify JSON body
  .fetch();

const result = response.getContentAsJSON(); // Auto-parsed JSON
Logger.log(result.id);

Method chaining lets you declare headers and body concisely and send a POST request.

Best Practice – Pagination + Retry

function fetchAllPages() {
  let url = 'https://api.example.com/items?page=1';
  const allItems = [];

  HttpRequestApp.retrier({tries: 3, delay: 2000}) // Retry up to 3 times
    .paginate({                                   // Automatically walk pages
      predicate: res => JSON.parse(res.getContentText()).next, // returns next page URL
      processor: res => {
        const json = res.getContentAsJSON();
        allItems.push(...json.items);
        return json.next; // return next page URL if any
      }
    })
    .get(url)
    .fetch();

  Logger.log(`Total items: ${allItems.length}`);
}

Combining retry control and pagination produces a resilient, efficient data-fetch workflow.