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.HTTPResponseStart 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.
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.HTTPResponseShow example code
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.HTTPResponseShow example code
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.HTTPResponseShow example code
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
- getAllHeaders(): { [key: string]: string | string[] }
getAs
- getAs(contentType: string): Blob
getBlob
- getBlob(): Blob
getContent
- getContent(): number[]
getContentText
- getContentText(): string
- getContentText(charset: string): string
getHeaders
- getHeaders(): { [key: string]: string }
getResponseCode
- 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