[OH3 / Javascript] Trying to create a http PUT request, but failing?

Actually my working rule looks like this:


var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
var HTTP=Java.type("org.openhab.core.model.script.actions.HTTP");
var openHAB_PID =Exec.executeCommandLine(parseDuration("1s"), "pgrep", "-f", "openhab.*java");
logger.info("openHAB_PID = {}", openHAB_PID);
var headers = [];
headers["Authorization"] = "Bearer MyAPIToken";
headers["WWW-Authenticate"] =  "Basic";

systemstatus_thing=HTTP.sendHttpGetRequest("http://127.0.0.1:8080/rest/things/systeminfo%3Acomputer%3Aopuspi4",headers, 10000);
//logger.info( "systemstatus_thing_original: {}", systemstatus_thing);
systemstatus_thing=systemstatus_thing.replaceAll('"pid":([0-9]+)','"pid":' + openHAB_PID);
logger.info("before Post request");
var returnvalue = HTTP.sendHttpPutRequest("http://127.0.0.1:8080/rest/things/systeminfo%3Acomputer%3Aopuspi4", "application/json", systemstatus_thing,headers, 10*1000);
//logger.info("returnvalue: {}", returnvalue);

you need to replace MyAPIToken with your API Token and opuspi4 with your system-name.

1 Like