How to do HTTP Request in JSR223 Javascript in OH 2.5

  • openHAB version: 2.5.9-1

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?

It seems that your Java class path is not correct. In OH 2.5 it should be like this

var HTTP = Java.type("org.eclipse.smarthome.core.model.script.actions.HTTP");

Thanks, but it still doesn’t like it for some reason:

java.lang.ClassNotFoundException: org.eclipse.smarthome.core.model.script.actions.HTTP

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);

When I try to import the class using that namespace I get the “java.lang.ClassNotFoundException” error. :slightly_frowning_face:

When I try to access a different class in another rule, I get the same error:

  var Things = Java.type("org.eclipse.smarthome.model.script.actions.Things"); 

(with or without “core”, it makes no difference). I think I’m missing something.

Guess what… I removed the class stuff and just called “sendHttpPostRequest”… and it works :man_facepalming:

Sometimes the simple answers are the best.

Still, any idea why I can’t use the openHab core Java classes?

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);

Still no idea how to get at org.eclipse.smarthome.model.script.actions.Things, though.

I’m going to try this out and see what happens.

https://openhab-scripters.github.io/openhab-helper-libraries/Getting%20Started/Installation.html

Many/most of the org.openhab.core classes were located at org.eclipse.smarthome.core in OH 2.5.

For both 2.5 and 3.0, it’s already imported as actions. You don’t need to load it nor do you need to Java.type it. To call the MQTT Action

actions.get("mqtt", "id:of:mqtt:broker").publishMQTT("topic", "message");

Actions is made available for you automatically.

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);

Or do I have to wait until I migrate to OH3?

I also tried:

var Things = Java.type("org.openhab.core.model.script.actions.Things")

No joy.

I’m seeing lots of examples on the forum referring to ThingAction as the class name instead of Things.

I’ve seen that too, but in the context of DSL rules. I’m always unsure how any DSL examples transfer over to JSR223.

I just found this:

https://openhabforum.de/viewtopic.php?t=967&start=10

“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. :thinking:

Maybe I’ll just need to park this one until I get around to migrating.

In the helper libraries, in ActionExamples.js, I find this code commented at the end:

/**
 * ThingAction
 * ThingStatusInfo getThingStatusInfo(String thingUid)
 */
/* DOES NOT FIND ThingAction anymore
var ThingAction = Java.type('org.eclipse.smarthome.model.script.actions.ThingAction');
JSRule({
	name: me+" ThingAction",
	description: "TEST L:"+__LINE__,
	triggers: [ 
		//TimerTrigger("0/15 * * * * ?")
	],
	execute: function( module, input){
		logInfo("################ "+me+" Line: "+__LINE__+"  #################");		
		print("ThingAction getThingStatusInfo of': 	" + ThingAction.getThingStatusInfo("chromecast:chromecast:6a01c5ef0f0a6b3fd15849eda1379ab8"));	
	}
});*/