4
votes

I am using UrlFetchApp in a google apps script to perform a get, as follows:

var optAdvancedArgs = {
    "method": "GET",
    "headers": {"Cache-Control": "no-cache", "Pragma": "no-cache"}, 
};
var response = UrlFetchApp.fetch(url, optAdvancedArgs);

Despite my attempt to disable the cache in the headers, the response I get is always a cached copy. If I perform a wget in my console using the same url, I see receive an up to date version.

My question is: How can I really disable the cache when performing a UrlFetchApp.fetch? Is there something wrong with my code?

1
What URL are you trying to fetch and how are you confirming its a cached copy? This is strange indeed. The UrlFetch happens from the Google Data Centers and could be something funny happening with the data source trying to cache for that IP? - Arun Nagarajan

1 Answers

10
votes

I was able to overcome this issue by using "max-age=0" in my Cache-Control header, e.g.:

var url = "http://www.stackoverflow.com";
var options =
  {
    // Ensure we get a fresh copy of the site every time.
    headers : {'Cache-Control' : 'max-age=0'}
  };
var response = UrlFetchApp.fetch(url, options)

It sounds like Google App Engine has a similar problem. Someone opened an issue however it appears to have been closed.