Created
September 9, 2015 02:39
-
-
Save mogsdad/a76c32231a2b91ff8b59 to your computer and use it in GitHub Desktop.
Google Apps Script template functions for external host communication with UrlFetchApp. See https://mogsdad.wordpress.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* httpGetTemplate.gs | |
* | |
* A pair of template functions for external host communication with | |
* UrlFetchApp, including parameter encoding and error handling. | |
* | |
* From: gist.github.com/mogsdad/a76c32231a2b91ff8b59 | |
* | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
/** | |
* Template function for performing HTTP GET, with error handling. | |
* | |
* From: gist.github.com/mogsdad/a76c32231a2b91ff8b59 | |
*/ | |
function httpGetTemplate() { | |
// URL for target web API | |
var url = 'https://example.com/apis/--apiId--'; | |
// For GET method, API parameters will be sent in the URL. | |
// Start with an object containing name / value tuples. | |
var apiParams = { | |
// Relevant parameters would go here | |
'param1' : 'value1', | |
'param2' : 'value2' // etc. | |
}; | |
// For GET, Encode params object as a URI escaped query string | |
var encParams = toHtmlQuery_(apiParams); | |
// Construct `fetch` params object | |
var params = { | |
'method': 'GET', // This is redundant; GET is default | |
'muteHttpExceptions' : true | |
}; | |
var response = UrlFetchApp.fetch(url+encParams, params) | |
// Check return code embedded in response. | |
var rc = response.getResponseCode(); | |
var responseText = response.getContentText(); | |
if (rc !== 200) { | |
// Log HTTP Error | |
Logger.log("Response (%s) %s", | |
rc, | |
responseText ); | |
// Could throw an exception yourself, if appropriate | |
} | |
else { | |
// Successful POST, handle response normally | |
Logger.log( responseText ); | |
} | |
} | |
/** | |
* Template function for performing HTTP POST, with error handling. | |
* | |
* From: gist.github.com/mogsdad/a76c32231a2b91ff8b59 | |
*/ | |
function httpPostTemplate() { | |
// URL for target web API | |
var url = 'https://example.com/apis/--apiId--'; | |
// For POST method, API parameters will be sent in the | |
// HTTP message payload. | |
// Start with an object containing name / value tuples. | |
var apiParams = { | |
// Relevant parameters would go here | |
'param1' : 'value1', | |
'param2' : 'value2' // etc. | |
}; | |
// All 'application/json' content goes as a JSON string. | |
var payload = JSON.stringify(apiParams); | |
// Construct `fetch` params object | |
var params = { | |
'method': 'POST', | |
'contentType': 'application/json', | |
'payload': payload, | |
'muteHttpExceptions' : true | |
}; | |
var response = UrlFetchApp.fetch(url, params) | |
// Check return code embedded in response. | |
var rc = response.getResponseCode(); | |
var responseText = response.getContentText(); | |
if (rc !== 200) { | |
// Log HTTP Error | |
Logger.log("Response (%s) %s", | |
rc, | |
responseText ); | |
// Could throw an exception yourself, if appropriate | |
} | |
else { | |
// Successful POST, handle response normally | |
Logger.log( responseText ); | |
} | |
} | |
/** | |
* Generate HTML query string from given object | |
* Adapted from http://stackoverflow.com/a/18116302/1677912 | |
*/ | |
function toHtmlQuery_(obj) {return "?"+Object.keys(obj).reduce(function(a,k){a.push(k+'='+encodeURIComponent(obj[k]));return a},[]).join('&')}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment