The UrlFetchApp class
The UrlFetchApp
class can be used to issue HTTP/HTTPS request and get responses to/from any websites/URL. For example, the UrlFetchApp.fetch ("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=PACKT")
code returns the search result with the search term PACKT
. The response will be a raw content along with HTTP headers.
If the communication between the UrlFetchApp
class's fetch
method and the URL is okay then the URL should return a response code 200
. Otherwise, any other number corresponds to the type of error. We can check the response code before proceeding further using the getResponseCode
method.
You can get the content text using the getContentText
method and parsing it to JSON objects. The following code snippet pulls the content text and parses to JSON:
var url = "https://ajax.googleapis.com/ajax/services/search /web?v=1.0&q=PACKT"; try{ var resp = UrlFetchApp.fetch(url); if(resp.getResponseCode() == 200){ var text = resp.getContentText...