RESTful API help

hi,
could would like to run a script on home assistant via a command on my pc.
i’ve looked at the RESTful API and can confirm when i run

curl -X GET -H “x-ha-access: mypassword” -H “Content-Type: application/json” http://192.168.0.13:8123/api/

i get a call back saying API running,

yet when i run

curl -X POST -H “x-ha-access: mypassword” -H “Content-Type: application/json” -d ‘{“entity_id”: “script.lightandtelly_on”}’ http://192.168.0.13:8123/api/services/script/turn_on

i get the following error
curl: (3) [globbing] unmatched close brace/bracket in column 24
{“message”: “Data should be valid JSON”}

could someone please tell me what I’m doing wrong?

Looks like you are using a word processor for your commands. Your single tics and double tics are replaced with stylized open and closed quotes. Replacing these symbols with the simple single and double tics solves the problem.

Here is the result:

curl -X POST -H "x-ha-access: mypassword" -H "Content-Type: application/json" -d '{"entity_id": "script.lightandtelly_on"}' http://192.168.0.13:8123/api/services/script/turn_on
1 Like

thank you for the reply but i’m still getting the same error message

you could try it without the single quotes.
it expects json and you give a string.

Still no good :frowning:
Same error

i just looked at it again and its complaining about column 24.
i think the problem lies in how you present the password.

or maybe you have something in your password that breaks the command.
that could be something like a / \ or "or ’

thats what i can come up with (but i never used curl, so if thats not it i hope someone else see the problem)

The password it not an issue as I use it with ifttt webhook

I’ve experienced similar punctuation mark troubles, so I went a different route.

https://home-assistant.io/developers/python_api/

If the machine you’re using the curl command on can run python3 then the above will help you write a little python script that calls Hass just the same. You’ll need to install Hass to the machine, but you don’t have to run it - just to get the remote python module (well, that’s how I did it).

A sample of how I made mine work

#!/usr/bin/env python3
import homeassistant.remote as remote

api = remote.API('192.168.1.13', '[password]')
domain = 'media_player'
switch_name = 'media_player.hifi_pi'

media_link = remote.get_state(api, 'sensor.hifipi_volume')

if media_link.state == "unknown": media_link.state = "40"

counter = int(float(media_link.state))

remote.set_state(api, 'sensor.rotary_encoder_value', new_state=counter)

You’ll probably want to go with remote.call_service rather than remote.set_state in my example, there’s some more ideas in the link. It will definitely work, and it’s how I avoided long curl commands with quotes all over.

I’ve tested in on OSX and the command runs without issue… Are you using windows? Someone else had a similar problem last week (note the use of only double-quotes):

2 Likes

hi yes I’m using windows I no longer get an error stating
curl: (3) [globbing] unmatched close brace/bracket in column 24

yet I’m still getting the below error
{“message”: “Data should be valid JSON”}

the problem is the when I use
{“entity_id”: “script.lightandtelly_off”}
in ifttt webhook its works fine

Can you copy and paste the whole command (and error) you’re using? Thanks

curl -X POST -H "x-ha-access: password" -H "Content-Type: application/json" -d "{ "entity_id": "script.lightandtelly" }" http://192.168.0.13:8123/api/services/script/turn_on

“message”: “Data should be valid JSON”}

Try

curl -X POST -H "x-ha-access: password" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.lightandtelly\" }" http://192.168.0.13:8123/api/services/script/turn_on

~Cheers

5 Likes

:smile:
It works :smile:

Thank you

2 Likes

The problem is your quotes. I don’t know what editor you are using but you need something that is Linux friendly. Notepad ++ for Windows.??

I am using notepad +

PhyberApex post works

"{ \"entity_id\": \"script.lightandtelly\" }

Thank everyone for helping me out

I understand. Your post with your version of the command shows odd quotes. You need to figure out why your editor is using them and change settings etc for the future???

I just edited my above post
What fixed the problem was adding \ within the json

Adding “I’m using windows” would have gotten you to a solution much faster.

2 Likes

you are very right, a mistake I’m ashamed to have made.

sorry guys :frowning:

1 Like