**ARGH**&#@^%$##$% I must be blind, why this error on such a simple request?

I have a simple file named test.py:

from requests import post

url = "http://localhost:8123/api/states/input_text.my_data"
headers = {"Authorization": "Bearer < then my huge long token is here >", "content-type":  "application/json"}
data = {"state": "25"}

response = post(url, headers=headers, json=data)
print(response.text)

…and when I said above:

< then my huge long token is here >

I just meant that is replaced with just the hugely long string I have generated in home assistant as a long lasting token, with no additional characters around it (not the < and not the > )… so how come when I run:

sudo python3 test.py

My input_text.my_data in Home Assistant stays “unknown” and never gets assigned the value of “25” and I get this error message instead - ?

pi@kruse-pi:/usr/share/hassio/homeassistant$ sudo python3 test.py
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 704, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 450, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request
    httplib_response = conn.getresponse()
                       ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/lib/python3.11/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 788, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/six.py", line 718, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 704, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 450, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request
    httplib_response = conn.getresponse()
                       ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/lib/python3.11/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/share/hassio/homeassistant/test.py", line 7, in <module>
    response = post(url, headers=headers, json=data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 547, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

So trying it another way:

curl \
  -H "Authorization: Bearer < then my huge long token is here >" \
  -H "Content-Type: application/json" \
  -d '{"state": "fgfgfgfg"}' \
  http://localhost:8123/api/states/input_text.my_data

it simply returns:

curl: (52) Empty reply from server

I disabled adguard, I have turned off firewalls, and yes there is already an input_text.my_data also already created in HA… but this is on the same machine (rpi4) anyway - HA Supervised - and this is being run comletely outside of HA directly on the same host from a putty session.

What am I doing wrong?

Have you tried the IP of the host instead of localhost?

I don’t have a system to test on but it might be that the container isn’t able to reply properly to your request as the source and destination IPs could be getting messes up as it passes through docker networking. Using the IP of the host (e.g. 192.168.1.10) would avoid that issue and help diagnose if it’s a networking or a HA issue.

Yes I tried the IP as well

I’ve actually got this part working well, I had the HA setupo forcing all API calls to it to use the duckdns url which I prefer to ensure everything is always up and running.

The other part of this (not addressed here) was for getting rid of numerous command line sensors that are doing a grep all on the same few log files (data then use to populate the above) and I wanted to remove those multipple repeating full file scans - with one daemon which would essentially tail the end of the log and only update the approperiate sensor that applies to the line added. I had used pygtail but when fired up that scans the whole file from the beginning (read-from-end=True as a parameter which should work according to the documentation is actually an unrecognized parameter). So I am switching over to inotify for that - will come back with another question in a different post if I still have issues