[OH3] ECMA Script for using sendHttpPostRequest

I try to convert a DSL rule to a ECMA script, but i’m getting stuck for the right strings:

DSL Rule:

val orangeassistPostURL = ""
val timeoutSec = 10
val orgeassistcmd = '{"request": "zet badkamer aan"}'

rule "Badkamer radio Aan"
    Item Schakel_Knop_Badkamer received command
    if(Schakel_Knop_Badkamer.state.toString == "single") {
        var result = sendHttpPostRequest(orangeassistPostURL, "text/plain", orgeassistcmd, timeoutSec*1000)

ECMA Script so far:

var HttpUtil = Java.type("org.openhab.core.io.net.http.HttpUtil")
var url = "";
var orgeassistcmd = '{"request" : "zet badkamer aan"}';
HttpUtil.executeUrl("POST", url, orgeassistcmd, "text/plain", 2000)

But this gives me this error:

2021-01-21 09:35:56.863 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 2
java.lang.ClassCastException: Cannot cast java.lang.String to java.io.InputStream

After changing orgeassistcmd to null, it’s working(POST send empty content), so I might need to change the format of orgeassistcmd?

As the error tells, your orgeassistcmd needs to be passed as a stream.

var stream = new java.io.ByteArrayInputStream(
    new java.lang.String(orgeassistcmd).getBytes("UTF-8") );

and then use stream instead of orgeassistcmd in the executeURL.

Instead of fighting with the low level stuff, why not use the openHAB sendHttpPostRequest Action like you did in the Rules DSL rule?

var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");
var url = "";
var orgeassistcmd = '{"request" : "zet badkamer aan"}';
var result = HTTP.sendHttpPostRequest(url, "text/plain", orgeassistcmd, 10*1000);

Good point! :+1:

Didn’t find this in any doc (yet?) was looking at this place: Actions | openHAB

Thanks for the reply!