Dimitri Leroux
Dimitri Leroux

Reputation: 1

Unable to parse x-www-form-urlencoded using Apps Script (Webhooks)

I'm trying to get POST request in Gsheet using Apps Script (as a Webapp)! It works well for JavaScript body POST type but when I tried with x-www-form-urlencoded there is an error.

function doPost(e) {

//Return if null
  if( e == undefined ) {
      console.log("no data");
      return HtmlService.createHtmlOutput("need data");
    }

//Parse the JSON data

  var event = JSON.parse(e.postData.contents);
  var data = event.data;


//Get the last row without data

  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = Math.max(sheet.getLastRow(),1);
  sheet.insertRowAfter(lastRow);

//Get current timestamp

  var timestamp = new Date();

//Insert the data into the sheet

  sheet.getRange(lastRow + 1, 1).setValue(event.bt_signature);
  sheet.getRange(lastRow + 1, 2).setValue(data.bt_payload);
  SpreadsheetApp.flush();

  return HtmlService.createHtmlOutput("post request received");}

Thanks!

Upvotes: 0

Views: 1724

Answers (1)

TheAddonDepot
TheAddonDepot

Reputation: 8974

For x-www-form-urlencoded content, POST data is stored in the parameter and parameters properties of the event object. See documentation.

Upvotes: 2

Related Questions