Skip to content

Object reference

HTTP response

UrlFetchApp.fetch() returns an HTTPResponse after calling a URL. Pass muteHttpExceptions: true so error status codes return an HTTPResponse you can check with getResponseCode() and getContentText().

UrlFetchApp.HTTPResponse

Start here

Get this object first

Fetch a web address with muteHttpExceptions turned on, so an error status code comes back as a normal response instead of stopping the script.

Code.gs
function fetchExampleApi() {
  const response = UrlFetchApp.fetch('https://api.open-meteo.com/v1/forecast?latitude=47.6&longitude=-122.3&daily=temperature_2m_max', {
    muteHttpExceptions: true
  });
  Logger.log(response.getResponseCode());
}

Then use it

Common actions

Choose an action, then open its example only when you need the code.

Check the status code

This reads the numeric status code and logs whether the request succeeded, without changing anything.

UrlFetchApp.HTTPResponse
Show example code
Code.gs
function logFetchStatus() {
  const response = UrlFetchApp.fetch('https://api.open-meteo.com/v1/forecast?latitude=47.6&longitude=-122.3&daily=temperature_2m_max', {
    muteHttpExceptions: true
  });
  const status = response.getResponseCode();
  Logger.log(status === 200 ? 'Success' : 'Request failed with status ' + status);
}

Parse the response as JSON

This reads the response text and parses it into an object your script can work with. It does not send or change anything.

UrlFetchApp.HTTPResponse
Show example code
Code.gs
function logParsedForecastDate() {
  const response = UrlFetchApp.fetch('https://api.open-meteo.com/v1/forecast?latitude=47.6&longitude=-122.3&daily=temperature_2m_max', {
    muteHttpExceptions: true
  });
  const data = JSON.parse(response.getContentText());
  Logger.log(data.daily.time[0]);
}

Read the response headers

This lists the header names and values sent back with the response and does not change anything.

UrlFetchApp.HTTPResponse
Show example code
Code.gs
function logFetchHeaders() {
  const response = UrlFetchApp.fetch('https://api.open-meteo.com/v1/forecast?latitude=47.6&longitude=-122.3&daily=temperature_2m_max', {
    muteHttpExceptions: true
  });
  const headers = response.getHeaders();
  Object.keys(headers).forEach((name) => {
    Logger.log(name + ': ' + headers[name]);
  });
}

Technical reference

Exact names and signatures

Generated runtime and signature facts stay separate from the beginner actions above. Open method details only when you need them.

At a glance

Service
UrlFetchApp
Reference symbol
UrlFetchApp.HTTPResponse
Object
HTTPResponse
Runtime
From signature data

Methods and signatures

Browse 7 methods

getAllHeaders

From signature data Google source
  • getAllHeaders(): { [key: string]: string | string[] }

getAs

From signature data Google source
  • getAs(contentType: string): Blob

getBlob

From signature data Google source
  • getBlob(): Blob

getContent

From signature data Google source
  • getContent(): number[]

getContentText

From signature data Google source
  • getContentText(): string
  • getContentText(charset: string): string

getHeaders

From signature data Google source
  • getHeaders(): { [key: string]: string }

getResponseCode

From signature data Google source
  • getResponseCode(): number

Need the whole workflow?

Return to a task guide

Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.

Browse Script utilities tasks