如何从 JS POST 或 node.js?

How to POST from JS or node.js?

我想从我的 JS 或 node.js 文件执行以下 POST 命令。

terminal.zsh
curl -L --data-binary @data/scrape.csv https://script.google.com/macros/s/#/exec

我可以使用以下代码从 node.js 文件成功写入 .csv 文件。

node.js
const ObjectsToCsv = require('objects-to-csv');
const itemsAsCsv = new ObjectsToCsv(formattedItems);
itemsAsCsv.toDisk(filePathCsv, { allColumns: true, });

我尝试了以下方法但没有成功。我希望看到数据命中我的 API 但我没有看到任何数据。

node.js
const request = require('request');
request({
  method: 'POST',
  preambleCRLF: true,
  postambleCRLF: true,
  uri: postUrl,
  multipart: {
    chunked: false,
    data,
  },
},

我做错了什么?

  • 您想使用 Node.js 将 CSV 文件上传到 Google Apps 脚本的 Web 应用程序。
    • 根据 https://script.google.com/macros/s/#/exec 的 URL,我了解到您正在使用 Google Apps Script 的网络应用程序。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

问题和解决方法:

不幸的是,在当前阶段,在Google Apps Script的Web Apps中,即使使用multipart/form-datamultipart/related,也无法解析接收到的数据。因此,在这种情况下,作为解决方法,CSV 数据作为数据发送。

修改后的脚本:

当你的Node.js的脚本被修改后,下面的修改怎么样?在使用此脚本之前,请设置 Web Apps 的 URL 和文件的文件名。

const fs = require("fs");
const request = require("request");
request(
  {
    url: "https://script.google.com/macros/s/###/exec", // Please set this.
    method: "POST",
    body: fs.createReadStream("./sample.csv"), // Please set this.
    followAllRedirects: true
  },
  function(err, res, body) {
    if (err) {
      console.log(err);
      return;
    }
    // console.log(res);
    console.log(body);
  }
);
  • 在这个修改后的脚本中,它假定CSV文件作为样本上传。示例 CSV 文件名为 sample.csv.
  • 当您运行此脚本时,CSV 文件会上传到 Web Apps。
  • followAllRedirects 是重定向。默认值为 false。因此设置了 true。这是访问 Google Apps 脚本的 Web 应用程序所必需的。
  • 如果要上传像data = { key1: "value1", key2: "value2" }这样的对象,请将body: fs.createReadStream("./sample.csv"),修改为body: JSON.stringify(data),。通过这种方式,可以在 doPost(e).
  • 处使用 JSON.parse(e.postData.contents) 检索和解析该值

Web 应用程序示例脚本:

function doPost(e) {
  var csv = Utilities.parseCsv(e.postData.contents);

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  sheet.appendRow([JSON.stringify(e)]);
  sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);

  return ContentService.createTextOutput("ok");
}
  • 通过上面的脚本,当示例Node.js脚本为运行时,上传sample.csv,上传的数据被Utilities.parseCsv()解析。然后,将解析后的值放入活动 Spreadsheet.
  • 的 sheet Sheet1
  • 如果分隔符不是,,请设置为parseCsv(csv, delimiter)

示例情况:

当使用上述 Node.js 脚本将以下 CSV 文件 (sample.csv) 上传到上述 Web 应用程序时,

a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
a3,b3,c3,d3,e3
a4,b4,c4,d4,e4
a5,b5,c5,d5,e5
a6,b6,c6,d6,e6
a7,b7,c7,d7,e7
a8,b8,c8,d8,e8
a9,b9,c9,d9,e9
a10,b10,c10,d10,e10

可以检索到以下事件对象。所以CSV数据可以解析为Utilities.parseCsv(e.postData.contents).

{
  "parameter": {},
  "contextPath": "",
  "contentLength": 165,
  "queryString": "",
  "parameters": {},
  "postData": {
    "type": "text/csv",
    "length": 165,
    "contents": "a1,b1,c1,d1,e1\r\na2,b2,c2,d2,e2\r\na3,b3,c3,d3,e3\r\na4,b4,c4,d4,e4\r\na5,b5,c5,d5,e5\r\na6,b6,c6,d6,e6\r\na7,b7,c7,d7,e7\r\na8,b8,c8,d8,e8\r\na9,b9,c9,d9,e9\r\na10,b10,c10,d10,e10\r\n",
    "name": "postData"
  }
}

注:

  • 当您修改Web Apps的脚本时,请重新部署Web Apps。由此,最新的脚本反映到Web Apps。

参考文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。