How to execute http POST command and read json output?

Hi all,

I have an INIM alarm system for which unfortunately there does not yet exist a binding. Nevertheless, there is an API interface possible through http POST. Unfortunately my knowledge about POST/GET and cURL and the likes is very limited, so hoping for some help here.

On the web I found that the alarm status can be read and written through http POST. There is a google chrome extension “Talented API tester” that I can use to test POST/GET commands. Via this method, I have succesfully been able to read and write the alarm status. Below a screenshot on how I do this:


So input as following:
Base URL: http://cgi-bin/web.cgi
Content-type: application/x-www-form-urlencoded
Body:

  • “tk”: my secret code, which i have blackened out
  • “mod”: zone or cmd or do_sce (depends on what I want to read or control from the alarm)
  • “par”: sce or now (depends on what I want to read or control from the alarm)
  • “qindex”: Not sure what it is, but always 0

This method works perfectly. I get a JSON response as following:

{"zone":  [
               {"lb": "01 fire basement", "tl": "0", "st": "1", "mm": "0", ...},
               {"lb": "02 entrance door", "tl": "1", "st": "1", "mm": "0", ...},
               {"lb": "03 storage door", "tl": "1", "st": "1", "mm": "0", ...},
               ...
               ]
}

“st” is the status of the contact. A value of 1 means the door is closed. A status of 2 means the door is open. This way I can also read when the alarm is sounding, when there is a fire alarm , when a door is opened, etc…

My question is: How can I get these values in Openhab? I would first need to execute a POST command, probably through a rule, to retrieve the JSON response. Then I would need to store the “st” response in an item.

I someone could help me, or at least give me some clues on where to start, that would be greatly appreciated.

Even better would of course be to have a binding developed for this INIM alarm system. Unfortunately I’m not an IT programmer or specialist. I’d be happy to provide input or do some testing. In any case, I’m grateful for the Openhab community here which is doing an excellent job here! I have Openhab running for several years now, currently at 25 bindings and still integrating more and more each year :slight_smile:

You have to use JsonPath:

1 Like

Thanks. That indeed helps to transform the JSON response and put the “st” value in an item.

Question still remaining is then how do I execute the POST command in openhab and put the JSON response in an item (string type I assume).

different options would be:

  • use the http binding together with JsonPath
  • use executeCommandLine to start a script that e.g. runs curl to do POST and jq to parse the result;
    use the REST API to write a value / values to an or different items

I checked the http binding before, but couldn’t really find a way to make the content-type “application/x-www-form-urlencoded” work. I get response that “application/x-www-form-urlencoded” is not an allowed parameter value for content-type. Only allowed values are: application/json, application/xml, text/html, text/plain, text/xml

What about using sendHttpPostRequest? After reading through this forum, this seems to be something that could work. Currently have the following code:

rule "Get JSON response from POST request"
when
		Time cron "0 0/1 * 1/1 * ? *" // every minute
then
var String MY_URL = 'http://192.168.2.123/cgi-bin/web.cgi'
var headers = "application/x-www-form-urlencoded"
var content = '{"tk": "<my secret code>", "mod":"zone", "par":"now", "qindex":"0"}'
val output = sendHttpPostRequest(MY_URL, headers, content)
    Alarm_zone_json.postUpdate(output)
end

Where Alarm_zone_json is an item string type.

Cannot get it to work yet… but I think I’m close

Ok, managed to get it working. I´ll post my solution later!

Please see following post where I have outlined my solution in detail (for those interested):