JavaScript examples that have worked for you. Put them here for others to use

I’m going to post some complete examples of simple functions here that I could have done with myself. All this is OH3 - a lot doesn’t work in OH2.5, works badly, or requires modifications.

function DoHttpGetRequest(sURL, sDesc, headers)
{
  //logInfo("DoHttpGetRequest " + sDesc + ": URL: " + sURL);

  var bAPICallOK = true;
  var response;
  var parsedResponse;

  try 
  {
    var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");    
    response = HTTP.sendHttpGetRequest(sURL, headers, 1000);      
    parsedResponse = JSON.parse(response);

    //logInfo("DoHttpGetRequest: " + sDesc + ": result: " + parsedResponse + "; unparsed result: " + response);
  } 

  catch(exception) 
  { 
    HandleHTTPException("DoHttpGetRequest", sDesc, exception, response);
    bAPICallOK = false;
  }     

  if (bAPICallOK)
  {
    return parsedResponse;
  }
  else
  {
    return null;
  }
}

POST is similar:

    var HTTP = Java.type("org.openhab.core.model.script.actions.HTTP");
    var result = HTTP.sendHttpPostRequest(sURL, sContentType, "", 10*1000);
    var objResult = JSON.parse(result);

HandleHTTPException is just a generic error-handling function I use to write a log entry.