doPost not working in Google app script

Viewed 9478

I came across various questions but none of them could solve my problem. I wrote a simple doPost() code in google app script:

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

Then I deployed it as a web app and pasted the url on hurl.it to make a post request. However, there is nothing being logged in the log and the response is 200 (Ok). I think it is not going inside this doPost() function. Can anyone guide as to what am I doing wrong here?

4 Answers

I struggled with this for AWHILE NOW and I finally got lucky. I use w3schools alot so I read fully on the form element and its attributes. The ACTION attribute seems to be the key in getting doPost(e) to work for me and GAS.

  1. Here's my HTML (removed opening and closing angle brackets)
<form 
action="https://script.google.com/a/[org]/macros/s/[scriptID]/exec" 
method="post" target="_blank" >

        First name: input type="text" name="fname"<br>
        Last name: input type="text" name="lname"<br>
input type="submit" value="Submit"
</form>
  1. Here's my doPost ( the Logger ran as well as the new window displaying e.parameter)
function doPost(e){
  Logger.log("I WAS RAN!!")
  if(typeof e !== 'undefined') {
    return ContentService.createTextOutput(JSON.stringify(e.parameter));
  }
}

One of the reason can be you are using a Rest client like Postman. It won't work, though I don't know the reason why.

Try with a 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>
Related