HTTP POST

If you have a question or need help, this is the place to be.
Post Reply
alonmalka
Posts: 29
Joined: Wed Dec 04, 2013 10:07 am

HTTP POST

Post by alonmalka » Wed Dec 25, 2019 8:52 am

Hi all,

I would like to use Python to send an HTTP Post.

I am basicly trying to implement the following:

$ 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

So far I tried this:

from requests import post

url = 'http://192.168.1.XX:8123/api/services/switch/turn_on'

headers = {'authorization': 'Bearer Token','content-type': 'application/json'}

body = {'entity_id': 'switch.fibaro'} <---------------------- What to do with this line??

txt04 = post(url, headers=headers)
print(txt04.text)

I would appreciate some help.

Thanks,
Alon

User avatar
kgschlosser
Site Admin
Posts: 5508
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: HTTP POST

Post by kgschlosser » Wed Dec 25, 2019 4:06 pm

what to do with it is in the example I provided to you a few hours ago.

Code: Select all

import requests
import json

headers = {
    "Authorization": "Bearer ABCDEFGH",
    "Content-Type": "application/json"
}

data= {
    "entity_id": "switch.christmas_lights"
}

requests.post("http://localhost:8123/api/services/switch/turn_on", headers=headers, data=json.dumps(data))
If you like the work I have been doing then feel free to Image

Post Reply