Reputation: 16112
Let's say I have the following CSV data:
fruit.csvfruit,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¶m2=2' \
--header 'content-type: application/json' \
--data '{"json": true}'
Specifically, what does the parameter string, headers and other options look like?
Upvotes: 3
Views: 9729
Reputation: 201378
https://script.google.com/macros/s/IDENTIFIER/exec?param1=1¶m2=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.
--data-binary
of the option of curl command for uploading the file.Utilities.parseCsv()
for parsing the CSV data.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 @
.--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");
}
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 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"
}
}
If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 2