How to do curl POST of CSV data? How to do curl POST of CSV data? curl curl

How to do curl POST of CSV data?


  • 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.

Modification points:

  • 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.

Sample curl:

curl -L --data-binary @sample.csv "https://script.google.com/macros/s/###/exec"
  • 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.

Sample script: Google Apps Script

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");}
  • 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).

Sample situation:

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,e1a2,b2,c2,d2,e2a3,b3,c3,d3,e3a4,b4,c4,d4,e4a5,b5,c5,d5,e5a6,b6,c6,d6,e6a7,b7,c7,d7,e7a8,b8,c8,d8,e8a9,b9,c9,d9,e9a10,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": {    "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"  }}

Note:

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

References:

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