0
votes

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?

1
In the case which uses POST method for Web Apps, when the curl command is used with -XPOST, such error occurs. In this case, POST method is run using -d "" instead of -XPOST. Namely, please test curl -L -d "" https://script.google.com/macros/s/$SCRIPT_ID/exec . The request body is the same between -XPOST and -d "". But the result is different. If this didn't resolve your issue. I apologize. - Tanaike
This solved the issue! And today I learned one new thing: even though curl helpfully says "Unnecessary use of -X or --request, POST is already inferred" when I do a request with -XPOST and a payload, its behavior is different in the presence of redirects. -XPOST forces all subsequent requests after a redirect to be made using POST. On the other hand, if we don't specify -XPOST, subsequent requests after the first POST are made as GET requests. I don't know if this is intended behavior, but it's certainly unintuitive. Thank you for the help, Tanaike! - Rui Gonçalves
Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer and accept it? By this, it will be useful other users who have the same issue. - Tanaike
Done. Thanks again! - Rui Gonçalves
Thank you for your response. I upvote it. - Tanaike

1 Answers

2
votes

It seems that the problem was with my usage of curl, on subtle differences between using -XPOST and not using it. As suggested by Tanaike, changing from:

curl -L -XPOST https://script.google.com/macros/s/$SCRIPT_ID/exec

to

curl -L -d '' https://script.google.com/macros/s/$SCRIPT_ID/exec

Solved the issue. Even though curl helpfully says "Unnecessary use of -X or --request, POST is already inferred" when I do a request with -XPOST and a payload, its behavior is different in the presence of redirects. -XPOST forces all subsequent requests after a redirect to be made using POST as a method. On the other hand, if I don't specify -XPOST, the requests after the first POST are made as GET requests. I don't know if this is curl's intended behavior, but it's certainly unintuitive.