Examples of http binding using session ID


does anyone have experience or examples of how to use the http binding in combination with session IDs.

I want to connect to a server with the http binding but for security reasons the serves doesn’t accept the usual username/password structure. It forces https and it forces the use of session IDs. So you authenticated with username and password, then you get a session ID back from the server and you use this session ID is all following requests.

I’m looking for examples on how to use the http binding to connect to such server.


The HTTP binding really isn’t setup to do this. You could probably make something work using the HTTP actions in rules. for example, create a rule to:

  1. log in and get the token
  2. make one or more HTTP requests using that token
  3. log out

I’ve done similar things in Python but this approach should work in Rules as well, so long as you are able to actually format the HTTP GETS, PUTS, and POSTS the way the API needs it.

Beyond that, you can implement the above in a script using curl and call that script using the Exec binding or executeCommandLine actions.

Finally, you could implement a new binding for the API you are interacting with. If it is a common and/or popular API I bet there would be some demand for it.

Rich thanks a lot, that’s what I was thinking as well. Might either do your suggestion login every time or login once keep using session ID till I get message session no longer valid and login again.

I have done it in php (see below just FYI the part to authenticate and to read the temperature with obtained session ID)

$html = implode(’’, file(‘http://X…X.X.X/api/auth/login.json?user=XXXXX&pwd=XXXXXXX));
$keywords = preg_split("/[\042]+/", $html);
$sessionid = $keywords[3];
$URL= “http://X.X.X.X/api/menutree/read_datapoint.json?SessionId=".$sessionid."&Id=1154”;
$html = implode(’’, file($URL));
$keywords = preg_split("/[\042]+/", $html);
$templiving = $keywords[9];

so will try putting it in rules and worst case exec the script. But it still isn’t a really nice and clear solution.

Rich I would love to build a binding but that won’t be for the next months I’m afraid. Problem is I never used java before (only perl and php) and am new to Openhab as well. So have some learning curve to go through. So will first get my Openhab up and running, build it on rules and afterwards dive into binding development.
The funny thing is this is actually to get functionality of a binding that existed before and for some reason was deleted (binding for Siemens webserver). So if only I could compile the source files to a jar file that would give a much cleaner solution than I will ever be able to build.

I recommended logging in everytime above because the code is WAY simpler to write.

I’m struggling to get this working. First I’m trying to extract the sessionid value in a rule

rule "temperatuur"
Time cron “* * * * * ?“
var String json= sendHttpGetRequest(“http://xx.xx.xx.xx/api/auth/login.json?user=USER&pwd=PASSWORD”)
var test = transform(“JSONPATH”,”$.SessionId”, json)
postUpdate(“Session”, test)

When I look at the session string it shows
{ “SessionId”: “c377db41-b664-4e30-af57-5df2e803bec7”, “Result”: { “Success”: “true” } }

So the full response, not just the SessionId value. What am I doing wrong in my transfor step? Do I need to import something in my rules file ?

Issue solved, discovered my transform services were not working, installing them, updating the addons.dfg and an update/upgrade eventually did the trick