Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

post - doPost not working in Google app script

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your implementation does not meet all the requirements needed for a web app. Here's an excerpt from the documentation (link):

Requirements for web apps

A script can be published as a web app if it meets these requirements:

  • It contains a doGet(e) or doPost(e) function.
  • The function returns an HTML service HtmlOutput object or a content service TextOutput object.

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 completeness, you must also redeploy your web App as a new version every time you make changes to the code. Redeploying under an already existing version does not work, you have to make a new version for your changes to take hold.

Also using the standard Logger.log to trace changes within doGet(e) or doPost(e) is unreliable with web apps as they are executed asynchronously. I would recommend logging your output to a spreadsheet. There is an awesome script library called BetterLog that extends the Logger API to do just that; it can be found at the following link:

https://github.com/peterherrmann/BetterLog


UPDATE 2018-07-18

Apps Script now supports StackDriver Logging which is accessible from the Apps Scripts editor's View menu.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...