Oliver2
(Oliver)
October 18, 2020, 8:43am
1
Could somebody please assist what to put instead of the placeholder XXXXX below in order to send a command to an item with javascript?
I need to do it this way - no cURL or anything else available.
I tried with “state”, “status”, “command”, “CMD”, “data”, “value” but with no luck
javascript:(
function()
{
document.body.innerHTML='
<form method="POST" action="http://192.168.178.61:8080/rest/items/Test_item/" enctype="text/plain">
<input name="XXXXX" value="ON">
</form>';
document.forms[0].submit()
})();
Have you tryed without a name?
You could try fetch()
var myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");
var raw = "ON";
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("http://192.168.178.61:8080/rest/items/Test_item", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Oliver2
(Oliver)
October 18, 2020, 11:00am
3
In this case the syntax is probably wrong as the script is not executed.
Background for this is, I need a one-liner to be executed from an IoT device which does not have an http post command option.
I tried your suggestion like the following but it does not work either
javascript:(
function()
{
fetch("http://192.168.178.61:8080/rest/items/Dunkelheit",
{method:'POST',
headers:{'Content-Type':'text/plain'},
body:'ON'})
})();
Can you share what the IoT device is?
Oliver2
(Oliver)
October 18, 2020, 11:09am
5
That is to me a generic solution if a device is not able to execute an http post command like a camera, etc.
Currently I am using the following:
http://IP:8080/basicui/CMD?Item=ON
but that will be probably obsolete in OH3.
Wolfgang_S
(Wolfgang_S)
October 18, 2020, 11:14am
6
Oliver2
(Oliver)
October 18, 2020, 11:49am
7
Thanks Wolfgang,
that helped as I understand now that I have to pursue denominator’s suggestion (using fetch instead of submit).
However, the idea from post #1 can be executed e.g. from a browser whereas I do not see a way to start the fetch command from a browser. Any idea on this?
Use Google to search javascript fetch
? This is not a search engine replacement not a site to train how to use the Internet.
Oliver2
(Oliver)
October 18, 2020, 12:23pm
9
Bruce_Osborne:
javascript fetch
No, you didn’t get me. I am not asking how to execute this script nor am I asking how to write it.
I am asking how to put this into an “one-liner” so that I can execute it e.g. from a browser.
Put this into your browser and you will se what I mean:
javascript:(function f(){fetch("http://192.168.178.61:8080/rest/items/Dunkelheit",{method:'POST',headers:{'Content-Type':'text/plain'},body:'ON'});f()})();
Oliver2
(Oliver)
October 18, 2020, 12:39pm
10
Got it.
When you enter this into your browser you can send a command to an item via REST API (http post method):
javascript:(function f(){return fetch("http://192.168.178.61:8080/rest/items/Name-of_item",{method:'POST',headers:{'Content-Type':'text/plain'},body:'ON'})})();
Thanks denominator and Wolfgang
Also working:
<html>
<head>
<script>
var myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");
var raw = "ON";
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("http://192.168.0.XX:8080/rest/items/MYITEM", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
</head>
<body>
</body>
</html>