Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
DJCordhose Avatar asked Oct 18 '12 13:10

DJCordhose


2 Answers

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)
like image 173
Sujay Phadke Avatar answered Sep 19 '22 11:09

Sujay Phadke


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
}
like image 25
Rami Rosenbaum Avatar answered Sep 17 '22 11:09

Rami Rosenbaum