'How to access body of post request in app script using doPost(request)

I need to access the raw body of a post request in Google App Script. I see there is something like

function doPost(request) {
  request.contentLength
}

that actually returns the right length of the raw content of the request body. So I thought there must be something to get the complete body, e.g. as a String.

I am not looking to access form field parameters that might be transferred using post.



Solution 1:[1]

I know this question is old, but a lot of things have changed and Google has made this available now. You can easily access the body of a POST request from doPost(e) using:

e.postData.contents

If you want to parse incoming JSON, use this:

JSON.parse(e.postData.contents)

Solution 2:[2]

From the Release Notes of May 9, 2013, use request.postData.getDataAsString():

function doPost(request) {
    var jsonString = request.postData.getDataAsString();
    var jsonData = JSON.parse(jsonString);
    sheet.appendRow([ 'Data1:' , jsonData.Data1 ]); // Just an example
}

Solution 3:[3]

You can access the raw body of a post by creating a Blob from the parameter. You can then call various methods on the Blob object such as getBytes() or getDataAsString(). The functions are listed here.

function doPost(event) {
  var contentBlob = event.parameter.thefile;
  var contentAsString = contentBlob.getDataAsString();
  var bytes = contentBlob.getBytes();
  // do something with the contents ...
}

Solution 4:[4]

I don't think it is currently possible to access the POST body, say to implement a REST service. I submitted an enhancement request for this; if you stumbled upon this one, feel free to star it to vote on that issue.

Solution 5:[5]

This should also work.

function doPost(e) {

  if(typeof e !== 'undefined')
    Logger.log(e.parameter);

}

Solution 6:[6]

The simplest way to find out all that you have access to is to print out the request object.

function doPost(request) {
  Logger.log(request); 
}

And then do a POST to your script's URL to see the log content

Solution 7:[7]

Depending on the nature of what you are trying do and what control you have over the origination of the post request, you may be able to integrate with the Drive API. If the post data can be sent to the user's Drive account as a file (containing raw JSON data for instance), your script could then locate that file, load it's content, do whatever you need with it (put it in a spreadsheet for example), and optionally trash the Drive file. This assumes, of course, that you have control over how the post request is sent. A workflow might involve:

  1. Client sends post request with data to user's Drive account (tag or name file to be easily identified later)
  2. Upon completion of Drive File creation, client sends a request to your script with parameter "method=consumeData" or such
  3. Your script checks the user's Drive account using the DocList service and retrieves any files uploaded for your script

I haven't tried this yet, but should work!

Solution 8:[8]

This should work.

const doPost = (request) => {
  const { postData: { contents, type } = {} } = request;

  if (type === 'application/json') {
      //...
  }
  else {
    // console.error("Data is well not formatted. Check if that's JSON")
  }
  return ContentService.createTextOutput(JSON.stringify({ id: 1 })).setMimeType(ContentService.MimeType.JSON);

};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rami Rosenbaum
Solution 3 Kalyan Reddy
Solution 4 avernet
Solution 5 Amit Agarwal
Solution 6 Srik
Solution 7 jwilkey
Solution 8 Maulik Pipaliya Joyy