DoPost not working in google application script

I came across various questions, but none of them was able to solve my problem. I wrote simple doPost()

code in google app script:

function doPost(e){
  Logger.log("Hello World");
}

      

Then I deployed it as a web app and pasted in the url.it url to make the post request. However, nothing is logged and the response is 200 (Ok). I think this is not part of this feature doPost()

. Can anyone follow what I am doing wrong here?

+6


source to share


4 answers


Your implementation does not meet all the requirements for a web application. Here is an excerpt from the documentation ( link ):

Requirements for web apps

The script can be published as a web application if it meets the following requirements:

  • It contains the function doGet(e)

    or doPost(e)

    .
  • The function returns an HTML HtmlOutput

    service object or a content service object TextOutput

    .

Here are some examples:

function doGet(e) {
  var params = JSON.stringify(e);
  return HtmlService.createHtmlOutput(params);
}

function doPost(e) {
  return ContentService.createTextOutput(JSON.stringify(e.parameter));
}

      

And just for the sake of completeness, you must redeploy your web application as a new version every time you make changes to your code. Redeploying under a pre-existing version does not work, you need to create a new version for the changes to take effect.



Also using the standard Logger.log

to track changes in doGet(e)

or is doPost(e)

not reliable for web applications since they run asynchronously. I would recommend writing your output to a spreadsheet. There is an awesome BetterLog scripting library that extends the Logger API; it can be found at the following link:

https://github.com/peterherrmann/BetterLog


UPDATE 2018-07-18

Apps Script now supports StackDriver logging, which can be accessed from the View menu of the Application Script Editor.

+8


source


for the version of the published url of the "exec" web application to run with any new changes, you must publish a new version each time to make changes to your script. It doesn't matter how small the change is. Instead of using it, Logger.log("Hello World");

I would write the value to a spreadsheet.

SpreadsheetApp.openById(id).getSheetByName(name).appendRow(['test']);

      



There are 2 different urls in your web app. One with "dev" at the end and the other with "exec" at the end. The "dev" version is always the current code. The "exec" version never changes unless you publish a new version.

+4


source


I have been struggling with this for RIGHT NOW and I finally got lucky. I use w3schools a lot, so I read completely about the form element and its attributes. The ACTION attribute appears to be the key to making doPost (e) work for me and GAS.

  1. Here is my HTML (removed open and close angular brackets)
form 
action = "https://script.google.com/a/ [[org ]/macros/s/ [scriptID ]/exec" 
method = "post" target = "_blank">

        First name: input type = "text" name = "fname" 
Last name: input type = "text" name = "lname"
input type = "submit" value = "Submit" form
  1. Here's my doPost (Logger starts and also a new window displaying e.parameter)

    function doPost(e){
        Logger.log("I WAS RAN!!")
        if(typeof e !== 'undefined') {
            return ContentService.createTextOutput(JSON.stringify(e.parameter));
        }
    
          

    }

+2


source


One of the reasons could be that you are using the Rest client like Postman. It won't work, although I don't know the reason.

Try it with normal form like this and it will work:

<!DOCTYPE html>
<html>
<body>

<form action="https://script.google.com/macros/s/AKfyc.../exec">
  First name:<br>
  <input type="text" name="param1" value="ABC">
  <br>
  Last name:<br>
  <input type="text" name="param2" value="XYZ">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>

      

0


source







All Articles