1
Solved

API POST to Home Assistant returns "Invalid JSON specified"

Hi,

I am trying to save a state to Home Assistant using the API.
The POST request is working fine in postman, but not i Tasker.

When i add the header "Authorization: Bearer <token>" (replacing "<token>" with the correct token. 183 characters consisting of letters and a period. So no special characters if that is relevant).
The response is "Invalid JSON specified".

If i change "Authorization" to something else it says "Unauthorized" instead.

What am i doing wrong?

2 replies

SB

I dont believe so. The JSON is very simple:

{
    "state": "Connected"
}

Postman accepts it and i tried this validator to see if i missed an invisible character or something - but it accepts it without errors.

Thanks. And you added all the needed headers as well? Can you show me how you're setting it up both in postman and Tasker?

SB

Is the following useful? Not sure what else to send?!

Here is a screenshot from tasker.

image-1617885600914.png

Postman body

image-1617885373954.png

Postman headers and response

image-1617885419519.png

Can you maybe try not setting the Content-Length parameter in Tasker and see if that helps?

SB

I have tried without Content-Length - same result. But it does not work in postman without it, so i thought i would include it.

The  "Invalid JSON specified" message doesn't come from Tasker so it must be something that's not reaching the server correctly. Can you try deleting all characters (select all > Delete) in the Body field and then just writing {} and see what that results in?

SB

That did the trick. Now it runs without any problems.
I thought it was Tasker that was hyper sensitive to the JSON and not Home Assistant (after doing a lot of testing in Postman). 

When i add anything more to the string it fails again. It seems i have to fiddle around with it to get it right.

Anyways - thank you for the assistance. Sorry i had to take up your time with a problem not related to Tasker.

And thanks for a great product. I have used Tasker for many years and its awesome! 👍😊

No problem, glad it works!

Maybe you have invalid JSON in your POST body?

Topic is closed for comments