首页 > 解决方案 > 无法使用 Apps 脚本 (Webhooks) 解析 x-www-form-urlencoded

问题描述

我正在尝试使用 Apps 脚本(作为 Web 应用程序)在 Gsheet 中获取 POST 请求!它适用于 JavaScript body POST 类型,但是当我尝试使用 x-www-form-urlencoded 时出现错误。

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");}

谢谢!

标签: javascriptgoogle-apps-script

解决方案


对于x-www-form-urlencoded内容,POST数据存储在事件对象的parameterparameters属性中。请参阅文档


推荐阅读