I’m having issues with calling the API from Python. I can use it fine from Workflows on my iPhone - but in Python the same call throws an error. What have I done wrong? Here’s my python code:
from requests import get, post
import json
payload = {'entity_id': 'switch.fence_lights'}
url = 'http://192.168.1.19:8123/api/services/light/toggle'
header = {'x-ha-access': 'secret','content-type': 'application/json'}
response = post(url, headers=header, data=payload)
print(response.text)
Which returns the following string:
http://192.168.1.19:8123/api/services/light/toggle
{'x-ha-access': 'secret', 'content-type': 'application/json'}
{'entity_id': 'group.playroom_lights'}
<html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1>Server got itself in trouble</body></html>
And here’s the error log:
17-05-20 17:56:18 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
File "/home/homeassistant/.homeassistant/deps/aiohttp/web_protocol.py", line 417, in start
resp = yield from self._request_handler(request)
File "/home/homeassistant/.homeassistant/deps/aiohttp/web.py", line 289, in _handle
resp = yield from handler(request)
File "/usr/lib/python3.4/asyncio/coroutines.py", line 143, in coro
res = yield from res
File "/usr/lib/python3.4/asyncio/coroutines.py", line 143, in coro
res = yield from res
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/http/ban.py", line 58, in ban_middleware_handler
return (yield from handler(request))
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/http/__init__.py", line 425, in handle
result = yield from result
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/api.py", line 313, in post
data = json.loads(body) if body else None
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)