Storing JSON data back from a POST request

I am trying to use the HTTP request cards to send a POST request to a web service and parse the output and store a variable. I’ve been looking for a solution on the net but can’t find anything.

My use case is to call a public transport real time web service using POST, giving it some details like the bus service/trip, and that service returns JSON that includes the current stop number. I want to store that current stop number into a Better Logic variable, then run a difference calculation on it using my home stop number and the stop number in the variable and then announce how many stops away on a smart speaker.

I’ve got all the ingredients working to do this, the only problem is that the HTTP request cards seemingly can only parse a GET command, not a POST command. The real time web service only accepts POST.

Am I missing something?

Have solved my problem with the awesome help from @robertklep over on the Slack channel

For the HTTP request cards, you can specify advanced options in the URL box - and the gold was that you can include the payload/body as a json in that url string. So what I’ve done is:

1 flow that does the web service call - using a GET JSON trigger object. Using advanced options you enter the full string into the url field - so in my case I needed to do a POST and therefore the url field was: {“method”:“post”,“protocol”:“https:”,“host”:“SITE.HERE”,“path”:"/a/nz-akl/realtime",“json”:{“stop_id”:1234,“trips”:{“45678”: {“departure_time_seconds”:20,“route_short_name”:“267”}}}, “headers”:{“Accept”:“application/json”, “Content-type”:“application/json”}}

I then set up a second flow with a JSON object trigger, and in the action I used a JSONpath Better Logic card to extract the JSON to a variable which I then calculate the number of stops to my bus stop. Also if in the first flow the web service returned no results (ie bus not operating on that route yet, then I set the number of stops to 0)

Then in a third flow I had a Better Logic variable trigger on change, and this plays a message to a smart speaker using a Home Assistant command announcing the number of stops

I hope this helps someone else along the way

2 Likes

Hi @CitizenxNZ,

Very nice job to show the JSON trigger object in plain text in flow 1, would it please also be possible to show how you’ve solved to pass the JSON object to a Better Logic variable in similar plain text?
This would help me and a lot of people struggling with their implementation of JSON in Homey.

I just tried exactly your flow and i get this error:
image
Can you help please?
Looks like you can’t use json in url string anymore.