#HTTP Request

110 messages · Page 1 of 1 (latest)

plucky phoenix
#

Can't seem to find a good bit of documentation (that makes sense) regarding how http requests are done and the responses parsed.
I'm looking to:

  • Send an http request (or restful call)
  • Somehow capture the JSON response and trigger an event to read/parse it
distant quail
plucky phoenix
#

So that requires installing the traefik addon?

distant quail
#

No that has nothing to do with traefik

#

that's just an example

#

you need to setup a rest_command integration

plucky phoenix
plucky phoenix
distant quail
#

Looks like you've made some progress.

#

To parse the response, you would follow that with another action with a template that uses the gotbindata variable.

plucky phoenix
#

my URL there returns JSON, but I can't see how to parse it

#

Ok

#

So, YAML coding only?

distant quail
#

Yes this will likely involve yaml/jinja.

plucky phoenix
#
response_variable:gotbindata
distant quail
#

Templates are generally not supported in the visual editor.

#

You can still use the overall visual editor, but the individual elements with a template will need to be edited in yaml.

plucky phoenix
#

So thats in automations

distant quail
#

Again you can still stay in the visual editor, you don't need to edit automations.yaml directly

plucky phoenix
#

Yep, however I'm trying to somehow get this response

#

somehow

#

and I don't see exactly where I'm supposed to do that

distant quail
#

Ok. Lets start from the beginning.
What do you want to do with this response.

plucky phoenix
#

🙂

#
  • So, every hour I want an http request to fire (rest or however) and that returns a JSON element, from my website
  • This JSON will contain some vars and values, and I really only want to see one value from that and do something with that value, basically looking for a 1 or 0
distant quail
#

Ok so go back to your visual editor. After the rest_command, add an if/then condition action (sorry).

#

Select add condition, then template condition

plucky phoenix
#
  • I also have a button on the dashboard to trigger the same event to force update (well)
plucky phoenix
distant quail
#

you can use your response variable there in the template (actually I guess it doesn't require yaml mode)

#

then you can do one thing or another thing based on some property of that response

plucky phoenix
#

Right, so Settings >> Automations

#

Then I'm here

distant quail
#

Yes. Now Add Action

#

there's one called if-then

plucky phoenix
#

Ah, I see

#

Right, so When >> trigger happens
Then do >> send restful command
then straight afterwards it automatically parses it and checks for value gotbindata in the response at that point?

distant quail
#

Everything in the action sequence after the restful command has a template variable called gotbindata available that it can read.

#

that will have your response json in it

plucky phoenix
#

In the "then" section, can I output a debug or log msg just to check "Value was 1" type message?

#

or "Value was 2"

distant quail
#

You can use persistent_notification.create, or logbook.log

#

Easier for debugging is maybe to just use the Trace viewer though

plucky phoenix
#

Ok, my logbook doesn't show much

distant quail
#

ok sorry use == not ===

plucky phoenix
#

Ok, that saves

distant quail
#

(I've been writing too much javascript 🤣)

plucky phoenix
#

Yeah, constantly switching languages is quite interesting!

#

Ok, I'm giving that a test. Thanks for your help so far

#

so far 😄

distant quail
#

Explore around in the trace viewer if you haven't played with it.

#

It's confusing at first but it's powerful for debugging.

#

Will show you what data it got back from the rest command and such

plucky phoenix
#

Ok, the trace viewer is very good

#

in >> Automations I did "Run actions" to manually trigger it, and then the trace viewer shows the whole JSON package returned into that variable

#

The next output however didn't fire

#

The "value was 1" in the UI is transposing to data: message: Value was 1 <--- does that need speechmarks in the YAML?

distant quail
#

Yaml doesn't require quotation marks for strings

plucky phoenix
#

ok

distant quail
#

so your template was not true

plucky phoenix
#

Yep

#

However JSON returned : ... "testrestval":"1" }

distant quail
#

Can you screenshot the full json response?

plucky phoenix
#

its format is {"aaa":"123", "bbb","123123", "ccc":"123123123", ... etc "testrestval":"1" }

distant quail
#

Maybe also try gotbindata["testrestval"] == "1"

#

could be forgetting the exact syntax

plucky phoenix
#

ok

#

(Sorry this has turned into a full conversation)

distant quail
#

no worries

#

Hmm well I'm willing to debug further if you're willing share the whole trace on a codeshare. I understand you might be trying to keep things secret, but I can't tell much more from here.

#

or you can try just using the example I posted above and try to work it out yourself

plucky phoenix
#

Sadly it's not working so still need an expert's help here. Not fussed about privacy at this stage. Happy to look at codeshare if it keeps this post from getting too wild

distant quail
#

it's probably soemthing simple

plucky phoenix
#

Totally, and likely something silly I've done

distant quail
#

use the "Download trace" button in the top right of the trace viewer under dotsvertical

stiff boughBOT
#

Please use a code share site to share code or logs, for example:

Please don't use Pastebin, since it can randomly add spaces to the main view. Please also don't share text as images since it makes it harder for people to help you. Remember that others may have colour blindness, impaired vision, etc.

plucky phoenix
#

Done. PM'd you codeshare link

distant quail
#

Ok a couple things. For one I guess the data is actually in gotbindata['content']['testrestval']

#

And another issue is that this data doesn't actually appear to be json, but it's like a stringified version of json

#

E.g. here's what I get back when I hit a json test server:

#

that's from

rest_command:
  example_jsontest:
    url: http://jsonplaceholder.typicode.com/users
#

Yours looks like this:

plucky phoenix
plucky phoenix
#

I've always output JSON the same way as other apps will "json parse" that string... I never realised

distant quail
#

maybe you need a content_type option in your rest_command to tell it that you want json

#

I'm not exactly sure

#

You could hack your data out of that string somehow, but it's a bit non-standard

plucky phoenix
#

Ok, I edited the PHP to add a json content type (oops)

#

and now the trace shows differently

#

I changed the condition variable format

#

So this is your variable format for the condition:

distant quail
#

🎉