Rest API from Curl command

Hello everyone :slight_smile:

Hope you are fine during this strange time.
I’m trying to call home assistant through a curl command.

The script I try to call is “Ampli”, it helps me using a Broadlink RM4C Mini to send a command to my Denon Amplifier.

Here is my script.
curl -X POST -H "Authorization: Bearer mytoken" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.Ampli\" }" http://192.168.1.2:8123/api/services/script/Ampli

I get the error “400: Bad Request”, do you have any idea what could provoke this error ?

Thanks :slight_smile:
Manu

Authorisation should be by access token not password - RestAPI

Of course I use access token, I just replaced it in my exemple by the word “password”

“Authorization: bearer yourtoken”

EDIT

B is capital
“Authorization: Bearer yourtoken”

Yes I got this from the documentation and it’s ok already.

curl -X POST -H "Authorization: Bearer mytoken" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.Ampli\" }" http://192.168.1.2:8123/api/services/script/Ampli

The “bad request” error is coming from something else…

Thank you for your help :slight_smile:

I’ve just tested turning a light on using:

curl -X POST -H 'Authorization: Bearer <longtoken>' -H 'Content-Type: application/json'  -d '{"entity_id": "switch.kitchen_lamp_switch"}'  http://192.168.1.x:8123/api/services/switch/turn_on

which works fine here.

May not be root cause, but look at your nested quotes for the data section and compare with above.

1 Like

I will give more testings this afternoon.
When I take a look to your example, I think there’s something wrong with my API URL.

My URL: http://192.168.1.2:8123/api/services/script/Ampli (“Ampli” is the name of the script I created)
Maybe it should be only http://192.168.1.2:8123/api/services/script/ as the entity id refers already to the specific script I want to call (“Ampli”)

In fact I’m not clear what’s expected by Home Assistant as URL& Entity ID to call a script.

look at Developer Tools>>Services page in your homeassistant.

you need to call a valid service available there. I dont use scripts so I not sure what is valid service to call script but you may look there to verify.

i did find strange you get 404 for bad password. I think you still wrong format password however.

OK you’re right I will try to call the service directly and skip the script part even if my script executes perfectly from Home Assistant UI.

Off the top of my head, you probably want something like:

curl -X POST -H 'Authorization: Bearer mytoken' -H 'Content-Type: application/json' -d '{"entity_id': "script.Ampli"}' http://192.168.1.2:8123/api/services/script/turn_on

Thanks TazUK, this is much better now with the URL you suggested.

curl -X POST -H "Authorization: Bearer Mytoken" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.Ampli\" }" http://192.168.1.2:8123/api/services/script/turn_on

Now curl command executes without error but my Ampli script is not executed.
After checking logs in Home Assistant UI I see that.

Logger: homeassistant.helpers.service
Source: helpers/service.py:189
First occurred: 16:07:13 (2 occurrences)
Last logged: 16:07:25

Unable to find referenced entities script.ampli

Probably the entity id is not good now.

EDIT: Ok I get it now…I must use the numeric identifier of my script (I got it in scripts.yaml configuration file).

Good syntax now is

curl -X POST -H "Authorization: Bearer Mytoken" -H "Content-Type: application/json" -d "{ \"entity_id\": \"script.1589020271119\" }" http://192.168.1.2:8123/api/services/script/turn_on

Works like a charm !!! :slight_smile:

Thank you! Your example helped me finding out this out for the last week.

1 Like