CodeStudio
Collection of programming code examples
Colab
Machine Learning
Neural Network
Python
What Is Programming?
Are you sure that you got it right?
Saturday, September 25, 2021
Apps Script Example: doGet doPost fetchUrl
/* --------------------------- */ function doGet(e){ if (e.parameter.payload) { //assuming json-formatted data var parsedData=JSON.parse(e.parameter.payload); return outputData(parsedData); }; return outputData(99) } function doPost(e){ if (e.postData) { //assuming json-formatted data var parsedData=JSON.parse(e.postData.contents); return outputData(parsedData); }; return outputData(9) } /* --------------------------- */ function outputData(data){ return ContentService.createTextOutput(JSON.stringify( { "wappstus": "success", "wappdata": data } )).setMimeType(ContentService.MimeType.JSON); } /* --------------------------- */ function postData(){ var objtData = { "name" : "Me Testing", "slug" : "me-testing", "text_to_subscribe" : "true" }; var payload = JSON.stringify(objtData); var options = { "method" : "POST", "contentType" : "application/json", "payload" : payload }; //var url = "https://postman-echo.com/post"; var url = ScriptApp.getService().getUrl(); var textJson = UrlFetchApp.fetch(url, options).getContentText(); Logger.log(textJson); var objtJson = JSON.parse(textJson); Logger.log(objtJson); } /* --------------------------- */ function getData(){ var objtData = { "name" : "Me Testing", "slug" : "me-testing", "text_to_subscribe" : "true" }; var options = { "method" : "GET", "contentType" : "application/json" }; //send object data as encoded string //var url="https://postman-echo.com/get?payload="+encodeURIComponent(JSON.stringify(objtData)); var url = ScriptApp.getService().getUrl()+"?payload="+encodeURIComponent(JSON.stringify(objtData)); var textJson = UrlFetchApp.fetch(url, options).getContentText(); Logger.log(textJson); var objtJson = JSON.parse(textJson); Logger.log(objtJson); } /* --------------------------- */
. . .
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment