UrlFetch 负载的 POST JSON 文件

问题描述 投票:0回答:1

我正在尝试使用 Google Apps 脚本和 UrlFetchApp 从我的 Google Drive JSON 文件发布 JSON 数据。 当我直接将文件的 JSON 字符串作为脚本中的变量传递时,它就可以工作。当尝试首先下载 JSON 文件以获取字符串或对象时,我丢失了一些内容。我查看了 JSON.stringify 文档以获取使用文件的示例,但没有运气。所有示例都使用脚本本身中存在的字符串。

以下是 JSON 文件内容的示例:

{
  "TotalAmt": 50.0, 
  "BillEmail": {
    "Address": "[email protected]"
  }, 
  "CustomerMemo": {
    "value": "Thank you for your business and have a great day!"
  }
}

这是我的 POST 请求:

var myJSONfile = "https://drive.google.com/someJSONFILE.json"

    var response = UrlFetchApp.fetch(url, {
      method: 'POST',
      muteHttpExceptions : true,
      payload: JSON.stringify(myJSONfile),
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken(),
        'Content-Type': 'application/json',
        Accept: 'application/json'
         }
    });

尝试直接传入 JSON 文件,但 POST 调用不起作用。

javascript google-apps-script google-drive-api urlfetch
1个回答
0
投票

首先尝试使用 url 读取文件,将其内容提取到变量

myJSONfile
中。

总的来说,其余代码应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.