Reputation: 11
I want to use curl and json to send a new row to a SmartSheet. I practiced simpler commands so know my token and sheet id are ok. I cant make the following code run. It would help to see similar code populated with all the variables. The code was pasted from the Smartsheet api help for AddRow. Code:
curl https://api.smartsheet.com/2.0/sheets/1451528792893316/rows -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -X POST -d '[{"toTop":true, "cells": [ {"columnId": 6554165443880836, "value": "VICTOR"}, {"columnId": 4302365630195588, "value": "NP8888"} ] }, {"toTop":true, "cells": [ {"columnId": 6554165443880836, "value": "VICTOR"}, {"columnId": 4302365630195588, "value": "NP0009"} ] }]' -o error.txt pause
I am running this from a windows batch file.
The error is:
{ "errorCode" : 1008, "message" : "Unable to parse request. The following error occurred: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: REST input; line: 1, column: 2]", "refId" : "oixvszudmnjc" }
I can't see a mismatched quote or brackets. Whats wrong ?
Chris B
Upvotes: 1
Views: 934
Reputation: 2823
It appears that curl or the command prompt is not handling single quotes correctly. The fix is to do one of the following:
Wrap your JSON in double quotes and then escape any double quotes in the JSON string.
"[{\"toTop\":true, \"cells\": [ {\"columnId\": 6554165443880836, \"value\": \"VICTOR\"}, {\"columnId\": 4302365630195588, \"value\": \"NP8888\"} ] }, {\"toTop\":true, \"cells\": [ {\"columnId\": 6554165443880836, \"value\": \"VICTOR\"}, {\"columnId\": 4302365630195588, \"value\": \"NP0009\"} ] }]"
Use double quotes in the JSON string and pipe the string to curl while having curl read from standard input via the @- parameter. This option means you won't need to escape each double quote.
echo [{"toTop":true, "cells": [ {"columnId": 6554165443880836, "value": "VICTOR"}, {"columnId": 4302365630195588, "value": "NP8888"} ] }, {"toTop":true, "cells": [ {"columnId": 6554165443880836, "value": "VICTOR"}, {"columnId": 4302365630195588, "value": "NP0009"} ] }] | curl https://api.smartsheet.com/2.0/sheets/1451528792893316/rows -H "Authorization: Bearer TOKEN" -H "Content-Type: application/json" -X POST -d @-
Use a better shell such as cygwin
On a side note pause
should be on a new line and not part of the curl command.
Upvotes: 2
Reputation: 9335
Seems to work from a bash shell (at least it doesn't give a parsing error). You mentioned a "windows batch file", I suspect that using the DOS shell is tripping up the encoding here. Try using a cygwin bash shell/terminal window instead.
Upvotes: 1