0

I'm writing a bash script to interface with my ecobee (query info and change settings). I have the authorization all worked out (access token and refresh token) and am now trying to request info from the ecobee. This json parameter list is dynamically created. None of the curl examples in the Developers API Doc seem to work.

I've tried assigning the json to a variable (?json="$selection") and to a file (?json=@"$location"). My latest attempt (hard coding the json and escaping the braces) of the curl is as follows:

response=$(curl -s "https://api.ecobee.com/1/thermostat?json=\{"selection":\{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true/}/}" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $access_token")

and I received a null response declare -- response=""

If I have curl read from a file: {"selection":{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true}}

then I receive:

response='{
  "status": {
    "code": 4,
    "message": "Serialization error. Malformed json. Check your request and parameters are valid."
  }
}'

Which I assuming it' an escape issue?

Can anyone offer any insight? Again, I thought this would be the easy part. I'm using jq to build my request json. Other alternatives to curl that can better deal with json? I'm limited to bash (which is what I know)

Thanks

rr0ss0rr
  • 154
  • 1
  • 6
  • You say you're composing the JSON query using jq. Try adding the `@uri` filter and the `-r` option, then use the output as inline URI parameter. For your example I get `%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D`. May be worth a try. – pmf Feb 14 '22 at 16:42
  • WOW! That worked! "?json=$(jq -r '@uri' ~/etc/ecobee.selection)". Please post this as the answer – rr0ss0rr Feb 15 '22 at 01:31

1 Answers1

2

To integrate (arbitrary) data into a URL (or URI in general) you need to prepare it using percent-encoding first.

As you have mentioned to use jq to compose that data, you could add @uri to your jq filter to perform the encoding, and use the --raw-output (or -r) option to then output it properly.

For example, jq -r '@uri' applied to your JSON

{
  "selection": {
    "selectionType": "registered",
    "selectionMatch": null,
    "includeRuntime": true,
    "includeSettings": true
  }
}

would output

%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D

which you can use within the query string ?json=….

pmf
  • 24,478
  • 2
  • 22
  • 31
  • I don't know ecobee at all, but chances are there's also another, more comfortable way to feed it with your JSON file (e.g. using [`POST`](https://en.wikipedia.org/wiki/POST_(HTTP)) instead of `GET` as request method), as the total length of the query string is limited and your JSON might grow over time. – pmf Feb 15 '22 at 12:30
  • Thanks again for the very detailed answer. Was banging my head on this for most of the weekend – rr0ss0rr Feb 15 '22 at 12:33
  • I found Ecobee’s API doc limited with Curl samples that don’t seem to work. – rr0ss0rr Feb 15 '22 at 12:38