I want to do the following POST
command from my JS or node.js file.
const ObjectsToCsv = require('objects-to-csv');
const itemsAsCsv = new ObjectsToCsv(formattedItems);
itemsAsCsv.toDisk(filePathCsv, { allColumns: true, });
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?
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.
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.
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);
}
);
sample.csv
.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.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)
.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");
}
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.,
, please set it as parseCsv(csv, delimiter)
. 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"
}
}
If I misunderstood your question and this was not the direction you want, I apologize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.