Wednesday, May 2, 2018

1001 Apps Script Web App URL Fetch

1001 Apps Script Web App URL Fetch



.

1001 Apps Script Web App URL Fetch
How to access other resources on the web by fetching URLs

Objective

1. Create doGet and doPost Method
2. Create testGet and testPost Method.
3. Create getRequest Method.

1. Create a stand-alone script

eg UrlFetchApp

2. Edit the script

The following codes were modified from the original codes published at: https://ctrlq.org/code/19871-get-post-requests-google-script 
function doGet(e) {

 if(typeof e !== undefined)
   return ContentService.createTextOutput(JSON.stringify(e.parameter));

}

function doPost(e) {

 if(typeof e !== undefined)
   return ContentService.createTextOutput(JSON.stringify(e.parameter));

}

function testPOST() {

 var url = ScriptApp.getService().getUrl();

 var payload =
     {
       "name" : "a",
       "blog" : "a.com",
       "type" : "post",
     };

 var options =
     {
       "method"  : "POST",
       "payload" : payload,  
       "followRedirects" : true,
       "muteHttpExceptions": true
     };

 var result = UrlFetchApp.fetch(url, options);

 if (result.getResponseCode() == 200) {
 
   var params = JSON.parse(result.getContentText());
 
   Logger.log(params.name);
   Logger.log(params.blog);
 
 }

}

function testGET() {

 var queryString = "?name=a&blog=a.com&type=get";

 var url = ScriptApp.getService().getUrl() + queryString;

 var options =
     {
       "method"  : "GET",  
       "followRedirects" : true,
       "muteHttpExceptions": true
     };
 
 var result = UrlFetchApp.fetch(url, options);

 if (result.getResponseCode() == 200) {
 
   var params = JSON.parse(result.getContentText());
 
   Logger.log(params.name);
   Logger.log(params.blog);
 
 }
}
function testGetRequest(){

  var url = ScriptApp.getService().getUrl();
  var payload =
      {
        "name" : "a",
        "blog" : "a.com",
        "type" : "get",
      };

  var params =
      {
        "method"  : "GET",
        "payload" : payload,  
        "followRedirects" : true,
        "muteHttpExceptions": true
      };  

 var response = UrlFetchApp.getRequest(url,params);
 for(i in response) {
   Logger.log(i + ": " + response[i]);
 }
}

3) Test

3.1) Potential Errors

You may get two potential errors if you do not set the following properties correctly.

1. Deploy the script as web app.

Otherwise you will get the following error messages.

DNS error: http://null?name=a&blog=a.com&type=get (line 60, file "Code")
testGet error messages
Attribute provided with no value: url (line 34, file "Code")
testPost error messages

2. Allow everyone to run the script.

Otherwise you will get the following error messages.

SyntaxError: Unexpected token: < (line 64, file "Code")
testGet error messages

3.2) Get Request Method

This method returns the request that would be made if the operation were invoked.

Copy:

https://script.google.com/d/1LOHXOPHxKZyFVrmjWgv_T_SuVU461HTnUs2y-jsv-iP-v3z6hHRKS2VO/edit?usp=sharing 

REFERENCES:

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

https://ctrlq.org/code/19871-get-post-requests-google-script 

.

visit link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.