如何卷曲CSV数据的POST? [英] How to do curl POST of CSV data?

查看:124
本文介绍了如何卷曲CSV数据的POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下CSV数据:

Say I have the following CSV data:

fruit,count,
Apples,152,
Bananas,23,

我如何编写以下形式的curl命令来发布该数据?

How would I write a curl command of the following form to post that data?

curl --request POST \
  --url 'https://script.google.com/macros/s/IDENTIFIER/exec?param1=1&param2=2' \
  --header 'content-type: application/json' \
  --data '{"json": true}'

具体来说,参数字符串,标头和其他选项是什么样的?

Specifically, what does the parameter string, headers and other options look like?

推荐答案

  • 您要使用curl命令将CSV文件上传到Google Apps脚本的Web Apps.
    • https://script.google.com/macros/s/IDENTIFIER/exec?param1=1&param2=2的URL中,我可以了解到您正在使用Google Apps脚本的Web Apps.
      • You want to upload a CSV file using curl command to Web Apps of Google Apps Script.
        • From the URL of https://script.google.com/macros/s/IDENTIFIER/exec?param1=1&param2=2, 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.

          • 在您的情况下,它使用curl命令选项的--data-binary来上传文件.
          • 在Web Apps中,它使用Utilities.parseCsv()来解析CSV数据.
          • In your case, it uses --data-binary of the option of curl command for uploading the file.
          • At Web Apps, it uses Utilities.parseCsv() for parsing the CSV data.
          curl -L --data-binary @sample.csv "https://script.google.com/macros/s/###/exec"
          

          • sample.csv是CSV文件名.使用@读取文件.
          • 通过--data-binary,可以通过包含换行符来上传CSV数据.如果使用-d,则不能包含换行符.所以请注意这一点.
          • -L是重定向.当它使用curl命令访问Web Apps时,需要使用此选项.这样,返回了ContentService.createTextOutput("ok")中的ok.
            • sample.csv is the CSV file name. The file is read with @.
            • By --data-binary, the CSV data can be uploaded by including the line break. If -d is used, the line break cannot be included. So please be careful this.
            • -L is the redirect. When it accesses to Web Apps using curl command, this option is required to be used. By this, ok of ContentService.createTextOutput("ok") is returned.
            • function doPost(e) {
                var csv = Utilities.parseCsv(e.postData.contents);
              
                var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
                sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
              
                return ContentService.createTextOutput("ok");
              }
              

              • 通过上述脚本,运行示例curl命令时,将上载sample.csv,并由Utilities.parseCsv()解析上载的数据.然后,将解析后的值放入活动电子表格的工作表Sheet1.
              • 如果分隔符不是,,请将其设置为parseCsv(csv, delimiter).
                • By above script, when the sample curl command 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).
                • 使用curl -L --data-binary @sample.csv "https://script.google.com/macros/s/###/exec"将以下CSV文件(sample.csv)上传到上述Web应用程序时,

                  When the following CSV file (sample.csv) is uploaded to the above Web Apps with curl -L --data-binary @sample.csv "https://script.google.com/macros/s/###/exec",

                  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": {
                      "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": ""
                    },
                    "contextPath": "",
                    "contentLength": 165,
                    "queryString": "",
                    "parameters": {
                      "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": [
                        ""
                      ]
                    },
                    "postData": {
                      "type": "application/x-www-form-urlencoded",
                      "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.
                    • --data-binary
                    • -L, --location
                    • parseCsv(csv)

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

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

                    这篇关于如何卷曲CSV数据的POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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