How do I do an HTTP request in Javascript in OH2.5? All the examples I’ve tried to follow give me a “java.lang.ClassNotFoundException” error for the HTTP bit.
I tried:
var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");
var URL = "xxxx";
var result = HTTP.sendHttpPostRequest(URL, "text/plain", "", 10*1000);
but it seems to be OH3-only.
I tried replacing the first line with:
var HTTP = Java.type("org.openhab.model.script.actions.HTTP");
and
var HTTP = Java.type("org.eclipse.smarthome.model.script.actions.HTTP");
but the result is the same. Do I need to include some helper libraries?
Not sure if you need the osgi functions of the OH script helper libraries.
Anyway it should also work without.
You could try it like this:
var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");
// Not sure if OH2.5 is still using eclipse packages, but I think not
//var HTTP = Java.type("org.eclipse.smarthome.core.model.script.actions.HTTP");
var http = new HTTP();
http.sendHttpPostRequest(URL);
I stumbled across a reference to actions.js. It turns out that it exports the HTTP class. So this include is enough to give you another way to make the HTTP request:
load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/lib/javascript/core/actions.js');
var URL = "xxx";
var result = HTTP.sendHttpPostRequest(URL, "text/plain", "", 10*1000);
OK, nice. Thanks. Specifically, is getThingStatusInfo(sThingUID) available anywhere? I tried calling the function separately and it doesn’t seem to be available. Trying to load the Java class gave me the “java.lang.ClassNotFoundException” error:
var Things = Java.type("org.eclipse.smarthome.model.script.actions.Things");
var status = Things.getThingStatusInfo(sThingUID);
“Was mir gerade so auffällt: ThingAction gibt es in 2.4 stable nicht mehr, du müsstest eigentlich auch eine Fehlermeldung im openhab.log sehen.”
Luckily for me, I understand German. For the benefit of anyone browsing this: he says that ThingAction seems to have vanished in 2.4. He then suggests that you can call the function directly:
if (getThingStatusInfo("max:bridge:NEQ1444904").getStatus() == "OFFLINE")
But in JSR223 this doesn’t seem to work, in 2.5 anyway.
Maybe I’ll just need to park this one until I get around to migrating.