Need assistance to use curl command to turn device ON/OFF

I am trying to follow the CURL example from:
https://developers.home-assistant.io/docs/external_api_rest/

Example:

curl -X POST -H "Authorization: Bearer ABCDEFGH" \
       -H "Content-Type: application/json" \
       -d '{"entity_id": "switch.christmas_lights"}' \
       http://localhost:8123/api/services/switch/turn_on

Below is the actual command-line I’m using on Windows. However, it returns a bunch of errors.

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjEzMzM3NzJkZjQ0Zjg0OWJiNmZkMGQyNDFlOTJjOCIsImlhdCI6MTU4NzgzNzIwMiwiZXhwIjoxOTAzMTk3MjAyfQ.iWy1Hbws1K8d7NPZdSTiqGe73QA2ojclFx-I1wppsWM" -H "Content-Type: application/json" -d "{\"entity_id\": \"light.media_room_light\"}" http://192.168.1.7:8123/api/services/switch/turn_off

It returns:

400: Bad Request

Could someone please tell me what specifically I’m doing wrong?

tl;dr: Take the backslash (\) characters out of your command. That is, you want to do this:

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9SSDSDIAIYUHDW(QUHDWE_WDd1iOu7f_c-qhTLFryN8E" -H "Content-Type: application/json" -d '{"entity_id": "light.media_room_light"}' http://homeassistant.local:8123/api/services/switch/turn_on

On your command line, \ is being used as an escape character, which means the double quotes are being passed literally instead of enclosing strings. So instead of curl seeing a -H flag with the string Authorization: Bearer ABCDEFGH, it gets a -H flag with the string "Authorization, followed by a separate argument Bearer, which it interprets as a hostname. hence the “Could not resolve host” errors.

Thank you. I am getting closer. Now, the only error I get is, 400: Bad Request. What am I doing wrong? I definitely have my long-lasting token and entity-id correct.

This is the command-line I’m using exactly character for character:

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjEzMzM3NzJkZjQ0Zjg0OWJiNmZkMGQyNDFlOTJjOCIsImlhdCI6MTU4NzgzNzIwMiwiZXhwIjoxOTAzMTk3MjAyfQ.iWy1Hbws1K8d7NPZdSTiqGe73QA2ojclFx-I1wppsWM" -H "Content-Type: application/json" -d "{\"entity_id\": \"light.media_room_light\"}" http://homeassistant.local:8123/api/services/switch/turn_on

I also enabled debug logging by adding: logger: to my configuration file. However, I don’t see anything in the System log when I POST API requests; and, get the response: 400: Bad Request. It seems like a major PITA just to do a simple API request using CURL.

EDIT: I also added an api: entry to my configuration.yaml file and rebooted. I wasn’t even sure if I was supposed to do that. However, that didn’t make any difference. I am still getting a 400: Bad Request response.

EDIT2: I found another person who appeared to have the same issue; except he was using javascript instead of CURL. His solution was to add a JSON.stringify() to the body of the POST. I have no idea what the CURL equivalent would be:
https://www.reddit.com/r/homeassistant/comments/80fd7r/help_using_the_api_to_post/

I just updated the original post with the new issue I’m facing. I would really appreciate help to troubleshoot this. I’m getting a 400: Bad Request response. Please see the command-line I’m using. I enabled debug logging, however, I don’t any information in the system log for API requests I’m making.

Try using \api\services\light, instead of switch, for light.media_room_light.

1 Like

Thank you so much! if it wasn’t for your response, I would have to give up completely!
I actually need to buy a Z-wave plug that turns OFF/ON appliances such as:
https://www.amazon.com/Z-Wave-Appliance-Repeater-Required-SmartThings/dp/B07Q1NFF6Z

Would I use “switch” instead of “light” for one of those?

That really depends on how it it set up in HA. Don’t have Z-wave myself so I can’t tell how it is normally recognized by HA.
After you get it and set up in HA, you should be able to find it in the Developer Tools\States and use the appropriate service.

Good luck.

Thanks to everyone on here! I finally found a way to ditch the IFTTT webhook I was using and turn off a plug though HA via batch file in Windows. I’m not sure I would have been able to get the context right if I hadn’t found this thread.

1 Like

I asked chatgpt for some help and I’m now using this curl command to fire a service on my home assistant (Volume up, there’s another for down)

#!/bin/bash

# Define your Home Assistant URL
HA_URL="myip:8123"

# Define your access token
ACCESS_TOKEN="my token"

# Define the service and command
SERVICE="remote"
COMMAND="send_command"

# Define the entity
ENTITY="remote.xiaomi_miio_192_168_1_98"

# Define the data in a separate variable
DATA='{"entity_id": "'"$ENTITY"'", "command": "volume_up", "num_repeats": 2}'

# Make the curl request with authentication
curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $ACCESS_TOKEN" \
     -d "$DATA" \
     "$HA_URL/api/services/$SERVICE/$COMMAND"