I'm trying to deploy a Google Apps Script as a web app, but while I have no problem doing GET requests, I'm having trouble with POST requests.
My code is very simple:
function doGet(request) {
var result = JSON.stringify({ data: 'Thanks, I received the GET request' });
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
function doPost(request) {
var result = JSON.stringify({ data: 'Thanks, I received the POST request' });
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
I deployed the web app with "Execute the app as: Me" and "Who has access to the app: Anyone, even anonymous". Every time I do some change I re-deploy it with a new version ("Project version: New").
After publishing, my curl GET request works perfectly:
> curl -L https://script.google.com/macros/s/$SCRIPT_ID/exec
{"data":"Thanks, I received the GET request"}
However, my POST request (curl -L -XPOST https://script.google.com/macros/s/$SCRIPT_ID/exec) just shows me a generic Google HTML page saying "Sorry, unable to open the file at this time. Please check the address and try again".
I tried sending some data and providing a content type, but nothing changed. I also tried changing the output type to just ContentService.createTextOutput("OK"), but it didn't work either. Curiously, deleting doPost changes the error message to "Script function not found: doPost" as expected. If it makes any difference, this script is attached to a Google spreadsheet.
Are there any special permissions I need to give to the script for POST requests specifically?
-XPOST, such error occurs. In this case, POST method is run using-d ""instead of-XPOST. Namely, please testcurl -L -d "" https://script.google.com/macros/s/$SCRIPT_ID/exec. The request body is the same between-XPOSTand-d "". But the result is different. If this didn't resolve your issue. I apologize. - Tanaike-XPOSTand a payload, its behavior is different in the presence of redirects.-XPOSTforces all subsequent requests after a redirect to be made usingPOST. On the other hand, if we don't specify-XPOST, subsequent requests after the firstPOSTare made asGETrequests. I don't know if this is intended behavior, but it's certainly unintuitive. Thank you for the help, Tanaike! - Rui Gonçalves