How to access post request body in script application using doPost (request)

I need to access the source body of a mail request in a Google Script application. I see that there is something like

function doPost(request) { request.contentLength } 

which actually returns the correct length of the raw content of the request body. So I thought there must be something to get a full body, for example. like a string.

I am not looking for access to form field parameters that can be passed using a message.

+4
source share
7 answers

From the May 9, 2013 release note, 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 } 
+8
source

I know these questions are old, but a lot has changed, and Google has made it available now. You can easily access the body of the POST request from doPost(e) using:

e.postData.contents

If you want to parse incoming JSON, use this:

JSON.parse(e.postData.contents)

+2
source

You can access the raw message body by creating a Blob from this 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 ... } 
+1
source

I do not think it is currently possible to access the body of a POST, say, to implement a REST service. I sent a request for improvement ; If you stumble upon this, feel free to vote for him on this.

+1
source

This should also work.

 function doPost(e) { if(typeof e !== 'undefined') Logger.log(e.parameter); } 
+1
source

The easiest way to find out everything you have access to is to print the request object.

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

And then send the POST to your script url to view the contents of the log

0
source

Depending on the nature of what you are trying to do and what kind of control you had when creating your mail request, you can integrate with the Drive API. If the mail data can be sent to the Drive user account as a file (for example, containing raw JSON data), your script can then find this file, load its contents, do whatever you need (put it in a spreadsheet for example), and possibly delete the disk file. This assumes, of course, that you control how the mail request is sent. The workflow may include:

  • The client sends a mail request with the data to the user. Drive account (tag or name file that will be easily identified later).
  • Upon completion of creating the disk file, the client sends a request to your script with the parameter "method = consumeData" or such
  • Your script verifies the user account using the DocList service and retrieves any files uploaded for your script

I have not tried it yet, but should work!

0
source

Source: https://habr.com/ru/post/1440553/


All Articles