Skip to content

Instantly share code, notes, and snippets.

@aelkz
Last active March 11, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aelkz/5cec5aa6753bdc007d3f to your computer and use it in GitHub Desktop.
Save aelkz/5cec5aa6753bdc007d3f to your computer and use it in GitHub Desktop.
/*
@description GOOGLE APPS SCRIPT - Simple REST method POST example with payload and header parameters.
@author raphael.alex@gmail.com
@github aelkz
*/
// receives a HTML form element for parsing authentication data.
function getBasicAuth(htmlForm) {
var userAndPassword = htmlForm.username+":"+htmlForm.password;
// encode the string to base 64
var x = Utilities.base64Encode(userAndPassword);
return "Basic " + x;
}
function persist(htmlForm) {
var url = "<REST POST URI>";
var digestfull = getBasicAuth(htmlForm);
var headers = {
"Accept":"application/json",
"contentType":"application/json",
"Authorization":digestfull,
"muteHttpExceptions":true
};
var payload = issue;
var options = {
"contentType":"application/json",
"method":"POST",
"headers":headers,
"payload":payload
};
try {
var resp = UrlFetchApp.fetch(url,options);
if (resp.getResponseCode() != 200) {
Browser.msgBox("Object creation failed.\\n\\nStack trace bellow:\\n"+resp.getResponseCode()+"\\n"+resp.getContentText());
return false;
}else {
// convert response to JSON object
return JSON.parse(resp.getContentText());
}
}catch(e) {
console.log("houston we have a problem");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment