httpcurlgoogle-apps-script

Google Apps Script: testing doPost() with cURL


I have created a very basic Google Apps Script. Here is my doPost() and doGet():

function doPost(e) {
  return respond(JSON.stringify({result: "Hello world!"}));
}

function doGet(e) {
  return respond(JSON.stringify({result: "Hello world!"})); 
}

function respond(response) {  
  return ContentService
  .createTextOutput(response)
  .setMimeType(ContentService.MimeType.JSON)
}

I have deployed the app with a new version. It is set to run as me but everyone can run it including anonymous.

I am testing the endpoint by running cURL from the command line, and for GET it works as expected:

Request -

curl -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec

Response -

{"result":"Hello world!"}

But for POST:

Request -

curl -H "Content-Length: 0" -D "p=p" -X POST -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec

Response -

Sorry, unable to open the file at this time.

Please check the address and try again.

As you can see from the command, I had to manipulate things a bit to get this far: including dummy data, adding a content-length header, and adding the -L flag to consume the redirect.

However, I am stuck. Why can't it "find the file"? Why does it think there is a file?


Solution

  • I suspect you'll need to send data.

    This works (I got {"result":"POST request received!"}):

    curl -H 'Content-Type: application/json' -d {} -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec
    

    As does this:

    curl -d "" -L https://script.google.com/macros/s/AKfycbxYxgMfMkR6hGI5UO2Gn8tg369oqsy_W41-olb0Do1y8gOjaNvm/exec