0

I'm using google app script to monitoring URL is working then writing log in excel after every 30 mins:

  try {
    var options = {
      "method" : "GET",
      'headers': {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
      },
      'muteHttpExceptions': true
    };
    var url_trimmed = url.trim();
    var response = UrlFetchApp.fetch(url_trimmed, options);
    Logger.log(response.getContentText()); 
    var result = response.getResponseCode();
    return result;
  }
  catch (err) {
    return [EXCEPT_ERR_CD, Utilities.formatString('statusCode: %s、Error: %s)', EXCEPT_ERR_CD, err)];
  }
}

But when I check Excel result file, result sometimes return responseCode 200, and sometimes return 403. Immediately when responseCode returns 403, I check it in browser but site is working fine. I tried add header parameter but it doesn't work. I dont have any idea, I think maybe the error from the site but dont really know causes and how to fix it.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
caocon912
  • 26
  • 6
  • It depends on the url you are fetching. I guess there is a limit rate by IP address so the script fails randomly. – idfurw Dec 13 '21 at 06:00
  • 1
    For example, about `Immediately when responseCode returns 403, I check it in browser but site is working fine.`, in this case, in that case, when you retry your script instead of the browser, what result will you obtain? Does the same error occur? – Tanaike Dec 13 '21 at 06:27
  • @Tanaike when retrying script immediately, the response code of site turns 200. It is not stable. I also trying other URLs, they still return 200 stable. So I guess it doesnt come from script. – caocon912 Dec 13 '21 at 06:37
  • Thank you for replying. In your situation, I thought that this thread might be the same issue. [Ref](https://stackoverflow.com/q/70118777/7108653) In that case, as a workaround, I proposed to retrieve the values from the URL by retrying UrlFetchApp. When you use this workaround, what result will you obtain? – Tanaike Dec 13 '21 at 06:40
  • @Tanaike Thank you for your recommend, I read and did it by retrying UrlFetchApp follows your answer but it seems no better :(, the response code still not stable. – caocon912 Dec 13 '21 at 08:22
  • Thank you for replying. I deeply apologize that my comment was not useful for your situation. This is due to my poor skill. I deeply apologize for this again. I have to study more. – Tanaike Dec 13 '21 at 08:26
  • @Tanaike No, please don't think so. You spend your time to think for my problem, I am so appreciate that. – caocon912 Dec 13 '21 at 08:39
  • Thank you. When I could think of another workaround, I would like to comment on it here. – Tanaike Dec 13 '21 at 11:31

0 Answers0