Receiving http/get requests to trigger items

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>
3 Likes

Hi, This would not work from IOT devices supporting only http get webhooks because those devices simply make a http GET request - however they don’t process the HTML received as the response. They don’t have browsers to do that :slight_smile:

Your solution probably works only when you use your browser to fire the mentioned

openhab:8080/basicui/CMD?item=command

link.

You can simulate http GET to check it in a way any devices would with a tool called curl or wget, etc.

Please check the bidning proposal here - [webhook] New, very simple binding for listening incomming http requests . This can be used with http-get-only webhook devices.

1 Like

That’s why I wanted to re-write the whole thing as a java servlet, but your http listener is exactly what I have been looking for for ages :slight_smile:
I will use it right away. Many thanks for your contribution and guiding me in this direction.

1 Like

How about making this great add-on available through this newly created marketplace?

I will try to make a pull request to main addon repository and have it in official distribution. Thx

(post deleted by author)