Search code examples
javascriptgoogle-apps-scriptgeticalendarurlfetch

Should I use .getContentText() in this Google Script GET request?


Both of these appear to return and log the same thing. Does it matter which is used? The first example is what's shown on the documentation.

var rawCalendar1 =  UrlFetchApp.fetch("https://example.com/blog/events.ics");   
Logger.log(rawCalendar1.getContentText());


var rawCalendar2 =  UrlFetchApp.fetch("https://example.com/blog/events.ics");
Logger.log(rawCalendar2);  

Solution

  • Return type for UrlFetchApp.fetch() is HttpResponse. In your example, you can call several methods on rawCalendar1 to inspect the returned value.

    See https://developers.google.com/apps-script/reference/url-fetch/http-response

    getContentText() converts the contents of HttpResponse to type 'string'. Likewise, Logger.log() casts what is between the brackets to 'string', which is essentially the same as calling getContentText() on it.