Google Scripts WebApp POST正文

时间:2016-11-15 16:52:31

标签: json post google-apps-script google-sheets

我觉得必须有一些显而易见的东西,但我无法理解Google WebApps处理POST请求的方式。

我的脚本有一个doPost(e)函数,可以收到Post请求,正如我在电子表格中看到的那样,但是当我尝试访问POST主体的内容时,它说它被称为“FileUpload”。我如何实际访问POST正文中的数据?

以下是我的GS代码;

function doPost(e) {
  var json = e.postData.contents()
  var data = JSON.parse(json)
  var test = e.contentlength
  var ss = SpreadsheetApp.openById("1GH1pT1BvJ-hMWrFNKUDD1KzBoEHd_TzKKk71Znk250g")
  SpreadsheetApp.setActiveSpreadsheet(ss)
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getRange("A1:A1");
  sheet.setActiveRange(range);
  range.setValue(e);
  range = sheet.getRange("A2:A2")
  range.setValue(json)
  range = sheet.getRange("A3:A3")
  range.setValue(test)
  range = sheet.getRange("A4:A4")
  range.setValue(data)
}

以下是我使用hurl.it发送的POST请求的正文

{
  "v": 1,
  "matches_filters": {
    "current": [
      10
    ],
    "previous": []
  },
  "meta": {
    "v": 1,
    "action": "added",
    "bullshit":"sooootrue"
    "object": "organization",
    "id": 3906,
    "company_id": 1146977,
    "user_id": 1718250,
    "timestamp": 1479142419,
    "permitted_user_ids": [
      1718250,
      1558089,
      1635656,
      1638428,
      1638433,
      1638438,
      1638443,
      1661971,
      1661996,
      1662001,
      1662006,
      1662011,
      1662016,
      1662021,
      1662026,
      1662031,
      1662036,
      1662041,
      1665942,
      1665954,
      1683795,
      1718611,
      1718619,
      1751500,
      1844875,
      1871026
    ],
    "trans_pending": false,
    "is_bulk_update": false,
    "matches_filters": {
      "current": [
        10
      ],
      "previous": []
    }
  },
  "retry": 0,
  "current": {
    "id": 3906,
    "company_id": 1146977,
    "owner_id": 1718250,
    "name": "Test Corporation [Dallas, TX, United States]",
    "open_deals_count": 0,
    "related_open_deals_count": 0,
    "closed_deals_count": 0,
    "related_closed_deals_count": 0,
    "email_messages_count": 0,
    "people_count": 0,
    "activities_count": 0,
    "done_activities_count": 0,
    "undone_activities_count": 0,
    "reference_activities_count": 0,
    "files_count": 0,
    "notes_count": 0,
    "followers_count": 0,
    "won_deals_count": 0,
    "related_won_deals_count": 0,
    "lost_deals_count": 0,
    "related_lost_deals_count": 0,
    "active_flag": true,
    "category_id": null,
    "picture_id": null,
    "country_code": null,
    "first_char": "t",
    "update_time": "2016-11-14 16:53:39",
    "add_time": "2016-11-14 16:53:39",
    "visible_to": "3",
    "next_activity_date": null,
    "next_activity_time": null,
    "next_activity_id": null,
    "last_activity_id": null,
    "last_activity_date": null,
    "address": "4 Main Street, Dallas, TX, United States",
    "address_lat": 32.7856712,
    "address_long": -96.773262,
    "address_subpremise": "",
    "address_street_number": "4",
    "address_route": "Main Street",
    "address_sublocality": "",
    "address_locality": "Dallas",
    "address_admin_area_level_1": "Texas",
    "address_admin_area_level_2": "Dallas County",
    "address_country": "United States",
    "address_postal_code": "75226",
    "address_formatted_address": "4 Main St, Dallas, TX 75226, USA",
    "040fd7fe54821ee6658b1079bdc0191037dcabb7": null,
    "ea3923782939325226d83a034fcfdcfd9bf14ac6": null,
    "ef5d2ea2f8ca035a742f0b520b35236811d3f9ab": "19",
    "7bd6a9cb46233100cdf6f46857394edcc128ed49": null,
    "c84920debaadd9619b0f0d5c6f6a32a0b69945ab": null,
    "4d83d5725c316f602420447b543b7cad93a4227e": "39",
    "edit_name": true
  },
  "previous": null,
  "event": "added.organization"
}

这是电子表格中记录的内容:

{parameter={}, contextPath=, contentLength=3080, queryString=null, parameters={}, postData=FileUpload}

我收到一条错误消息:

TypeError: Cannot call property contents in object FileUpload. It is not a function, it is "string". (line 9, file "Code", project "PipeDrive notifcations")

如果有人知道我做错了什么,我很乐意听到。非常感谢你!

1 个答案:

答案 0 :(得分:1)

所以我已经解决了我的问题。

我在postData上调用了.contents(),但是我应该写一下.contents。删除()修复了我的问题。