Diagnostic reference
Request failed for https://... returned code 4xx
Fix Apps Script UrlFetchApp.fetch() HTTP 400, 401, 403, 404, and 429 errors: use muteHttpExceptions, log the full response body, and check headers.
Updated November 26, 2026
Apps Script error
Exception: Request failed for https://api.example.com returned code 401. Truncated server response: ...
What it means
UrlFetchApp.fetch() throws an exception on any HTTP status outside 200 to 299 and truncates the response body. Status 401 or 403 usually means a missing or expired Authorization header. Status 400 points to a wrong contentType or malformed JSON payload, 404 means a wrong path or unencoded parameter, and 429 means the remote API rate-limited your requests.
Related error messages
- Exception: Request failed for https://... returned code 400.
- Exception: Request failed for https://... returned code 403.
- Exception: Request failed for https://... returned code 404.
- Exception: Request failed for https://... returned code 429.
- Exception: Request failed for https://... returned code 500.
Before and after
The code before and after the fix.
function fetchOrderStatus(orderId) {
const url = 'https://api.example.com/v1/orders/' + encodeURIComponent(orderId);
const response = UrlFetchApp.fetch(url);
return JSON.parse(response.getContentText());
}function fetchOrderStatus(orderId) {
const token = PropertiesService.getScriptProperties().getProperty('API_TOKEN');
if (!token) throw new Error('Set API_TOKEN in Script Properties first.');
const url = 'https://api.example.com/v1/orders/' + encodeURIComponent(orderId);
const response = UrlFetchApp.fetch(url, {
method: 'get',
headers: { Authorization: 'Bearer ' + token, Accept: 'application/json' },
muteHttpExceptions: true,
});
const status = response.getResponseCode();
const body = response.getContentText();
if (status < 200 || status >= 300) {
throw new Error('Order API returned HTTP ' + status + ': ' + body.slice(0, 300));
}
return JSON.parse(body);
}How to fix it
- Pass muteHttpExceptions: true in the options object so fetch() returns the HTTPResponse instead of truncating the error.
- Inspect response.getResponseCode() and response.getContentText() to read the full error from the remote API.
- For status 401 or 403, check that your token in Script Properties has no leading or trailing spaces and uses the header name the API expects.
- For status 400 on POST requests, set contentType to application/json and pass JSON.stringify(data) as payload.
- For status 429 or 5xx, pause with Utilities.sleep() before retrying instead of retrying immediately in a tight loop.
Things to check
- Never log Authorization headers, API keys, or tokens while debugging failed requests.
- UrlFetchApp.fetch() defaults to a 360-second timeoutSeconds value, which matches the 6-minute execution limit. Pass a shorter timeoutSeconds option for slow APIs, and keep daily per-account fetch quotas in mind.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes.