Skip to content

Instantly share code, notes, and snippets.

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 jrichardsz/14e09d8ae322f2d9aabb93cfaa014679 to your computer and use it in GitHub Desktop.
Save jrichardsz/14e09d8ae322f2d9aabb93cfaa014679 to your computer and use it in GitHub Desktop.
google app script as http rest api
function doGet(e) {
    var response = {
      "code": 200,
      "message": "hell world google app script"
    };
    return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON);
}

//this will not work and CORS error will appear in your browser console
//from backend, it works perfectly
function doPost(e) {
    var response = {
      "code": 200,
      "message": "hell world google app script"
    };
    return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON);
}

After the deployment as web app plus anonymous mode

image

You will have a ready to use http public url

This url is ready to use in the backend or frontend with these restrictions

{
"queryString": "",
"parameter": {},
"contextPath": "",
"contentLength": 197,
"parameters": {},
"postData": {
"contents": "{json here}",
"length": 197,
"name": "postData",
"type": "application/json"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment