How to POST from JS or node.js?

与世无争的帅哥 提交于 2019-12-25 01:37:25

问题


I want to do the following POST command from my JS or node.js file.

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

I can successfully write my .csv file from my node.js file with the following code.

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

I have unsuccessfully tried the following. I expect to see the data hit my API but instead I see no data.

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

What am I doing wrong?


回答1:


  • You want to upload a CSV file to Web Apps of Google Apps Script using Node.js.
    • From the URL of https://script.google.com/macros/s/#/exec, I could understand that you are using Web Apps of Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Issue and workaround:

Unfortunately, in the current stage, at Web Apps of Google Apps Script, even when multipart/form-data or multipart/related are used, the received data cannot be parsed. So in this case, as a workaround, the CSV data is send as data.

Modified script:

When your script of Node.js is modified, how about the following modification? Before you use this script, please set the URL of Web Apps and the filename of the file.

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);
  }
);
  • In this modified script, it supposes that the CSV file is uploaded as a sample. The sample CSV filename is sample.csv.
  • When you run this script, the CSV file is uploaded to Web Apps.
  • followAllRedirects is the redirect. The default value is false. So true is set. This is required to access to Web Apps of Google Apps Script.
  • If you want to upload an object like data = { key1: "value1", key2: "value2" }, please modify body: fs.createReadStream("./sample.csv"), to body: JSON.stringify(data),. By this, the value can be retrieved and parsed with JSON.parse(e.postData.contents) at doPost(e).

Sample script of Web Apps:

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");
}
  • By above script, when the sample Node.js script is run, sample.csv is uploaded, and the uploaded data is parsed by Utilities.parseCsv(). Then, the parsed values are put to the sheet Sheet1 of the active Spreadsheet.
  • If the delimiter is not ,, please set it as parseCsv(csv, delimiter).

Sample situation:

When the following CSV file (sample.csv) is uploaded to the above Web Apps with above Node.js script,

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

the following event object can be retrieved. So the CSV data can be parsed by 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"
  }
}

Note:

  • When you modified the script of Web Apps, please redeploy Web Apps. By this, the latest script is reflected to Web Apps.

References:

  • Web Apps
  • request
  • parseCsv(csv)

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/58849644/how-to-post-from-js-or-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!