如何从JS或node.js进行发布? [英] How to POST from JS or node.js?

查看:95
本文介绍了如何从JS或node.js进行发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

我没有成功尝试以下操作.我希望看到数据符合我的API,但我看不到任何数据.

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

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

我在做什么错了?

推荐答案

  • 您要使用Node.js将CSV文件上传到Google Apps脚本的Web Apps.
    • https://script.google.com/macros/s/#/exec的URL中,我可以了解到您正在使用Google Apps脚本的Web Apps.
      • 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.

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

          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.

          修改Node.js脚本后,以下修改如何?使用此脚本之前,请设置Web Apps的URL和文件名.

          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);
            }
          );
          

          • 在此修改后的脚本中,它假定CSV文件作为示例上传.样本CSV文件名是sample.csv.
          • 运行此脚本时,CSV文件将上传到Web Apps.
          • followAllRedirects是重定向.默认值为false.因此已设置true.要访问Google Apps脚本的Web Apps,这是必需的.
          • 如果要上载data = { key1: "value1", key2: "value2" }之类的对象,请将body: fs.createReadStream("./sample.csv"),修改为body: JSON.stringify(data),.这样,就可以在doPost(e)处使用JSON.parse(e.postData.contents)检索并解析该值.
            • 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).
            • 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()解析上载的数据.然后,将解析后的值放在活动电子表格的工作表Sheet1中.
              • 如果分隔符不是,,请将其设置为parseCsv(csv, delimiter).
                • 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).
                • 使用以下Node.js脚本将以下CSV文件(sample.csv)上传到上述Web应用程序时,

                  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
                  

                  可以检索以下事件对象.因此,可以通过Utilities.parseCsv(e.postData.contents)解析CSV数据.

                  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"
                    }
                  }
                  

                  注意:

                  • 修改Web应用程序的脚本后,请重新部署Web应用程序.这样,最新的脚本就会反映到Web Apps.
                    • Web Apps
                    • request
                    • parseCsv(csv)

                    如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

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

                    这篇关于如何从JS或node.js进行发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆