REST API Invalid JSON

Trying to interface with Home Assistant from a proprietary system and not able to get it to handle POST commands. This system doesn’t support tools such as cURL, so this content is being sent through an open TCP port raw.

My header seems fine as it authenticates and GET requests work fine. Different errors when the token is changed/missing. It’s only POST where I’m having issues with the JSON data and despite trying every variation I can think of, it’s still failing.

What I’m sending to Home Assistant:
POST /api/states/binary_sensor.test HTTP/1.1$0D$0AHost: 10.220.88.81$0D$0AAccept: application/json$0D$0AConnection: close$0D$0AContent-Type: application/json$0D$0AAuthorization: Bearer [TOKEN]$0D$0A$0D$0A{"state":"0","attributes":{"source":"HDMI"}}

What I’m receiving back from Home Assistant:
HTTP/1.1 400 Bad Request$0D$0AContent-Type: application/json$0D$0AContent-Length: 38$0D$0ADate: Wed, 22 Jan 2020 00:01:02 GMT$0D$0AServer: Python/3.7 aiohttp/3.6.1$0D$0AConnection: close$0D$0A$0D$0A{"message": "Invalid JSON specified."}

If anyone has ideas on what’s failing here, or how I can troubleshoot further, that’d be appreciated!

As far as I can see I’m formatting things in a way that should work, and match what I’m getting back.

Did you ever get this resolved?

Ahh yes I did, I don’t have the code on me now, but the problem was that I wasn’t defining the content length, once I included that in the header it was all working correctly.

Awsome cool, i worked out my issue but came accorss yours and thought if i cam up with a solution then i’d ping you if yours was not resolved.

Stay safe

1 Like