Let Me Tink About It
Let Me Tink About It

Reputation: 16122

How to extract POST body using GAS content service

This page describes how to extract parameters from POST requests using the GAS content service.

function doPost(request) {
  var events = CalendarApp.getEvents(
    new Date(Number(request.parameters.start) * 1000),
    new Date(Number(request.parameters.end) * 1000));
  var result = {
    available: events.length == 0
  };
  return ContentService.createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

But how does one extract the body of a POST request?

Upvotes: 0

Views: 537

Answers (2)

Spencer Easton
Spencer Easton

Reputation: 5782

You can access the POST body with the postData property of the event object.

https://developers.google.com/apps-script/guides/web#url_parameters

In your example:

function doPost(request) {
  var myData= request.postData; //myData is a blob
  .
  .
  .
  return ContentService.createTextOutput(JSON.stringify(result))
  .setMimeType(ContentService.MimeType.JSON);
}

The postData parameter contains a blob of the POST data. You can check the docs on blobs at:

https://developers.google.com/apps-script/reference/base/blob

Upvotes: 1

Wicket
Wicket

Reputation: 38296

From https://developers.google.com/apps-script/releases/2013

May 9, 2013

The following requested feature was added: Issue 2158: The request object passed in to doPost() methods now contains the POST body. It can be accessed using e.postData.getDataAsString().

Upvotes: 1

Related Questions