Send command to item with browser to REST API

Tags: #<Tag:0x00007efeca264f10>

I try to get away from passing a command to an item like

http://openhab:8080/basicui/CMD?Testitem=ON

(which works fine) and want to move to REST API. I have seen quite a few comments here, where this issue was addressed but not solved.
However, I still think there is a way of achieving this but need a little help for the last bit:

javascript:(function(){document.body.innerHTML='<form method="POST" action="http://openhab:8080/rest/items/Testitem"><input value="ON"/></form>';document.forms[0].submit()})();

In a browser this will execute a javascript containing a HTML “file” with all required information. I am still struggling how to correctly submit the command value “ON”.
I already get a response from OH-Server but with the following error message:

{"error":{"message":"HTTP 404 Not Found","http-code":404,"exception":{"class":"javax.ws.rs.NotFoundException","message":"HTTP 404 Not Found","localized-message":"HTTP 404 Not Found"}}}

The Restdocs submit with the browser.
In OH 3 though, many API parts required an authentication token forst.

Getting closer now:
After changing the request to

javascript:(function(){document.body.innerHTML='<form method="POST" action="http://openhab:8080/rest/items/Testitem" enctype="text/plain"><input id="Testitem" value="ON"/></form>';document.forms[0].submit()})();

openHAB Log Viewer gives me

Received HTTP POST request at 'items/Dunkelheit' with an invalid status value ''.

Does anybody know to which parameter the value needs to be assigned to?
The following do not work

  • CMD

  • command

  • state

  • value

  • Testitem

any other idea?

I would suggest to first send the command by running e.g. curl or wget command:

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://$openhab_ip:8080/rest/items/Testitem"

This works here.
Then use a network sniffer to capture the packages.
Then run a network sniffer to capture the packages from your javascript running in the browser and compare the content of both captured network transmissions.

2 Likes