-1

This question is about receiving POST request from somewhere. I'm looking for a google sheet script function that can take and handle data from the POST request in JSON format. Could you suggest any example?

The POST request is here:

https://script.google.com/macros/s/BOdirjv45Dn6FHrx_4GUguuS6NJxnSEeviMHm3HerJl4UsDBnDgfFPO/

{
  "p1": "writeTitle",
  "p2": [[URL]],
  "p3": [[PIC_A]],
  "p4": [[PIC_B]],
  "p5": [[TITLE]]
}

application/json

doPost() doesn't work:

doPost(e) {
var json = JSON.parse(e.postData.contents);
Logger.log(json);
}
  • Possible duplicate of [Google Apps Script make HTTP POST](https://stackoverflow.com/questions/14742350/google-apps-script-make-http-post) – Casper Oct 26 '18 at 13:14
  • 1
    Apps Script has the reserved functions names `doGet()` and `doPost()` which are event listeners for GET and POST requests made to the published Url of an Apps Script Web App. You can publish a script as an Apps Script Web App that is bound to a Google Sheet. (Or Doc or Form) So, to get code to run from a POST request made from some external source, you need to create a `doPost(e)` function. The parameter `e` will receive the payload object from the POST request. See "Event Objects" in the Apps Script documentation. – Alan Wells Oct 26 '18 at 14:01
  • @Sandy Good, thank you! I still don't understand how can I get access to sent data? I did doPost(e) { var json = JSON.parse(e.postData.contents); Logger.log(json); } and it shows nothing. –  Oct 26 '18 at 18:05
  • 2
    Log the value of `e` - `Logger.log(typeof e)` `Logger.log(JSON.stringify(e))` Also, you must publish the script as a Web App **EVERY** time that you make a change to the code. – Alan Wells Oct 26 '18 at 20:23
  • @Sandy Good, thank you very much! I added JSON.stringify to my initial json before sending a POST request and it works. But when I try to log Logger.log(typeof e) I see: "No logs found. Use Logger API to add logs to your project." –  Oct 27 '18 at 09:35
  • @I'-'I, thank you. –  Oct 27 '18 at 11:14

1 Answers1

2
  • You want to retrieve the value from the request body as an object.
  • You have already deployed Web Apps.

If my understanding of your situation is correct, how about this modification?

Post and retrieved object:

As a sample, I used the following curl command to POST to Web Apps.

curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec"

When above command is run, e of doPost(e) is as follows.

{
  "parameter": {},
  "contextPath": "",
  "contentLength": 90,
  "queryString": "",
  "parameters": {},
  "postData": {
    "type": "application/json",
    "length": 90,
    "contents": "{\"p1\": \"writeTitle\",\"p2\": \"[[URL]]\",\"p3\": \"[[PIC_A]]\",\"p4\": \"[[PIC_B]]\",\"p5\": \"[[TITLE]]\"}",
    "name": "postData"
  }
}

The posted payload can be retrieved by e.postData. From above response, it is found that the value you want can be retrieved by e.postData.contents. By the way, when the query parameter and the payload are given like as follows,

curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec?key=value"

value can be retrieved by e.parameter or e.parameters. And the payload can be retrieved by e.postData.contents.

Modified script:

In this modified script, the result can be seen at the Stackdriver, and also the result is returned.

function doPost(e) {
  var json = JSON.parse(e.postData.contents);
  console.log(json);
  return ContentService.createTextOutput(JSON.stringify(json));
}

Note:

  • When you modified your script of Web Apps, please redeploy it as new version. By this, the latest script is reflected to Web Apps. This is an important point.

Reference:

If this was not what you want, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you very much! I added JSON.stringify to my initial json before sending a POST request and it works. I can get back this data using doPost function. But I still can't see the log: console.log(json); does not show anything. –  Oct 27 '18 at 09:26
  • No logs found. Use Logger API to add logs to your project. –  Oct 27 '18 at 09:36
  • 1
    @Neret Thank you for replying. I'm glad your issue was solved. You can see the official document of Stackdriver at [here](https://developers.google.com/apps-script/guides/logging#using_stackdriver_logging). – Tanaike Oct 27 '18 at 11:58
  • 1
    @I'-'I Thank you for your support. – Tanaike Oct 27 '18 at 11:58