3

Let's say I have the following CSV data:

fruit.csv
fruit,count,
Apples,152,
Bananas,23,

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?

Marios
  • 26,333
  • 8
  • 32
  • 52
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • That isn't how CSV data looks or how you post it. Please review the [basic CSV formatting rules](https://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules) and [this answer](https://stackoverflow.com/questions/50998620/post-csv-text-file-using-curl) about how to post CSVs using curl. – Diego Nov 14 '19 at 01:25

1 Answers1

2
  • 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,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": {
    "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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Mowzer About [your previous question](https://stackoverflow.com/q/58847277/7108653), if you are required to use Node.js for uploading the data, please tell me. – Tanaike Nov 14 '19 at 03:25
  • Yes, I need imbed it in a node script because that is my scraping script. And I would like to write the file to my GAS API at the same time that I scrape the data. Because of convenience and keeping the writing operation synced with the scraping. – Let Me Tink About It Nov 14 '19 at 04:04
  • Here is an updated version of the original question. https://stackoverflow.com/q/58849644 – Let Me Tink About It Nov 14 '19 at 05:00