Skip to content

Invalid HTTP body (trailing null byte), causes aiohttp/HA to reject #2381

@adrianjagielak

Description

@adrianjagielak

First of all, just want to say thanks for all the work you've put into HAA. I've been using it for years to run a few HomeKit devices and it's been rock solid. Recently I started setting up Home Assistant and wanted to integrate my existing HAA-based devices without reflashing to another firmware.

While doing that, I ran into an issue where Home Assistant (which uses aiohttp python http server) rejects HTTP POSTs from HAA with a 400 error.

Example

Here’s the action I’m using in my config of a power meter service:

{
  "m": 2,
  "h": "192.168.1.157",
  "p": 8123,
  "u": "api/states/sensor.sensor_name",
  "e": "Authorization: Bearer XXX\r\nContent-Type: application/json\r\n",
  "c": "{\"state\":#HAA@0001}"
}

And this is the error I get in the Home Assistant logs:

Error handling request from 192.168.1.156
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/aiohttp/web_protocol.py", line 408, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "aiohttp/_http_parser.pyx", line 558, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadHttpMessage: 400, message:
  Data after `Connection: close`:

    b'{"state":0.0002333876}\x00'
                                ^

Looks like something might be going wrong with the HTTP payload formatting; maybe a null byte is being appended at the end, or something’s not handled right with the Connection: close behavior. Not 100% sure even who to blame.

I also tried using the "w" (wait for response) parameter to see if it’d help, but it didn’t make any difference in behavior.

It’s not a critical issue; I could work around it by using an intermediate script that proxies the requests to Home Assistant. I'm mainly reporting this because I couldn’t find any mention of it in existing issues, and it would be great if native HTTP POST worked smoothly.

If it’s possible to tweak the HTTP request formatting (e.g. omitting a trailing null byte if present, or checking how the body is sent), that’d help HAA devices play more nicely with strict HTTP servers like aiohttp.

Thanks again for the awesome project!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions