v0.2:
few changes
v0.1:
This is a commonly asked question if you have IoT devices which only support http GET
requests (like my Mobotix cameras) but OH3 requires http POST
method to trigger items from external.
There is an easy workaround to use the CMD servlet
openhab:8080/basicui/CMD?item=command
but that is not an official feature and subject to be dropped at any time.
There are also quite a few solutions for a get to post converter out there but to me they seem to be complicated (like installing a couple of packages and scripts, make changes to config files etc…).
That’s why I tried to find a replacement which is as easy as possible to implement, ideally without any additional apps.
Easy solution:
Below is an html code which you have to copy-paste into an .html
file and enter an API Token. Copy the file (any file name will do) to a directory of your choice in
openHAB-conf/html/
e.g.
openHAB-conf/html/CMD/index.html
That’s it!
the syntax is almost the same like the CMD servlet:
http://openhab:8080/static/CMD/index.html?item=command
I know, I could have done the code below even better, like making use of the constructor URLSearchParams()
but I wanted to have it as compatible as possible with older web clients like IE.
html code:
<!DOCTYPE html>
<html>
<head>
<title>get2post</title>
</head>
<body>
<script type="text/javascript">
const apiToken = "oh.get2post.tPmn1XUBj5EulsGg1TpIE15HRUhFpv0sUbPF2DydYF1ITOQ1LzHaqwkeFVPFdppUYK3YE0fByjGWKA"
const strHost = window.location.host; //replace with e.g. "openhab:8080";
const debug = true;
const strParam = (window.location + '').split("?");
if (debug) console.log("strHost: '" + strHost + "', strParam: '" + strParam[1] + "'");
//if ((strParam.length == 1) || (strParam[1] == "")) {
// console.log("Parameter not found");
//}
var item = strParam[1].split('=')[0];
var cmd = strParam[1].split('=')[1];
if (debug) console.log("item: '" + item + "', command: '" + cmd + "'");
if (window.XMLHttpRequest) {
//code for modern browsers
var req = new XMLHttpRequest();
} else {
// in case your IoT web client is based on an old IE engine
// this is NOT tested
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "http://" + strHost + "/rest/items/" + item);
req.withCredentials = true;
req.setRequestHeader("Content-Type", "text/plain");
req.setRequestHeader("Accept", "*/*");
req.setRequestHeader("Authorization", "Bearer " + apiToken);
req.onreadystatechange = function () {
console.log("Status: " + req.status);
}
req.send(cmd);
</script>
<h3>Usage:</h3>
<h4>http://openhab:8080/static/CMD/index.html?item=command</h4>
<h3>Installation:</h3>
<ul>
<li>create API Token in MainUI (<a href="https://www.openhab.org/docs/configuration/apitokens.html#openhab-api-tokens">see here)</a></li>
<li>edit index.html and enter API Token in line 7</li>
<li>create directory CMD in openHAB-conf/html/</li>
<li>copy index.html to openHAB-conf/html/CMD/</li>
</ul>
<h3>Debugging:</h3>
<ul>
<li>set debug to true in index.html, line 9</li>
<li>run URL within your browser and press F12 function key to see log output</li>
<li>if problems, enter IP address or host name (including IP port) in index.html, line 8</li>
</ul>
</body>
</html>