I’m trying to implement a REST sensor which first requires a POST authentication (https), then a GET to have the JSON dictionary returned. Can this be achieved in HA?
The first request is a login. It returns a html page which does not contain the data I’m looking for.
To get the JSON data, I need to issue a GET to a separate URL. Trying this with curl returns “you are not logged in”, even though the POST was successful. I guess I have to retain the login via a cookie or something?
Yes, that is typically how authentication works. Could be a cookie value, or a token in the HTTP response, which then needs to be supplied when performing the next request. I’d recommend using something like Chrome developer tools to analyse HTTP requests and responses and then try to emulate the same in a script or custom component.
Thanks, I’ve allready given Chrome dev-tools a go, but I’m not an expert at this. Seems the session i stored in a cookie. Was hoping to get to the data via a restful sensor, but I guess there’s no way around a multi-line script? Would you recommend running a python script for this?
So you need to sequence two calls, first to get a cookie and the second to get the data using the cookie. Yea REST sensor can’t do that, one call only.
Best way to do that is probably command line sensor. You can either make two curl calls one after another with the first getting the cookie used in the second or create a script that does what it needs to and call it. If you prefer python I’m pretty sure you can do that, just need to to output to stdout since that’s what the sensor will look for in value_template.
I have now stored the cookie using curl, and am able to retrieve the json data in a second call. Great! Now I have several options. Thanks for getting me on the right track!
Hi. I am trying exactly that, to sequence two curl calls using command line, but I couldn´t make it work yet. Have you tried it? Would you mind to share an example? Many tks!