UrlFetchApp.fetch is timeouting.

1,500 views
Skip to first unread message

Emre Sanli

unread,
Apr 7, 2023, 6:29:52 AM4/7/23
to Google Apps Script Community
Hi everyone,

Some URLs like i added into the script is loading forever and returning execution timeout error. I tried almost everything adding timeout etc. but i am not able to get response.
I tried to find alternative to UrlFetchApp but nothing there i can use.
Thank you for your help.

Here is the snippet of my code

function x(){

var options = {
'muteHttpExceptions': true,
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 200) {
Logger.log(response.getContentText());
} else {
Logger.log('Request failed: ' + response.getResponseCode());
}
}



Waqar Ahmad

unread,
Apr 10, 2023, 2:55:57 AM4/10/23
to Google Apps Script Community
May be, the server of the URL is just not returning any response for Google Apps Script IPs. They may have blocked to avoid scraping etc. This does not seem a Google Apps Script issue

Nerio Villalobos

unread,
Apr 11, 2023, 4:21:26 AM4/11/23
to google-apps-sc...@googlegroups.com
It's possible that the URL you are trying to fetch is not responding or is taking a long time to respond, which is causing your script to timeout. Here are a few things you can try:

Increase the timeout value: You can increase the timeout value of UrlFetchApp by setting the fetchTimeoutSeconds option in your options object to a higher value, like 60 seconds:

var options = {
  'muteHttpExceptions': true,
  'fetchTimeoutSeconds': 60
};

Use the UrlFetchApp.fetchAll() method: If you are fetching multiple URLs in your script, you can use the UrlFetchApp.fetchAll() method to fetch them in parallel. This can help speed up the overall execution of your script and reduce the chance of timeouts:

var urls = [  "https://www.example.com/page1",  "https://www.example.com/page2",  "https://www.example.com/page3"];
var responses = UrlFetchApp.fetchAll(urls);
for (var i = 0; i < responses.length; i++) {
  if (responses[i].getResponseCode() === 200) {
    Logger.log(responses[i].getContentText());
  } else {
    Logger.log('Request failed: ' + responses[i].getResponseCode());
  }
}

Use the UrlFetchApp.fetch(url, params) method: Instead of passing in options as a separate object, you can also pass them in as parameters directly to the UrlFetchApp.fetch() method. This can help simplify your code and make it easier to debug:

const url = "https://www.kansascity.com/news/business/article266505571.html";
const params = {
  'muteHttpExceptions': true,
  'fetchTimeoutSeconds': 60
};
const response = UrlFetchApp.fetch(url, params);

if (response.getResponseCode() === 200) {
  Logger.log(response.getContentText());
} else {
  Logger.log('Request failed: ' + response.getResponseCode());
}

If none of these solutions work, it's possible that the website you are trying to fetch is blocking requests from Google servers, or that there is a firewall or other security measure preventing your script from accessing the website. In this case, you may need to contact the website owner or IT department to see if they can whitelist your IP address or provide an API or other means of accessing the data you need.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d8e2a5f9-e883-490d-b327-36a2a09229d4n%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Reply all
Reply to author
Forward
0 new messages