Openhab 3 HTTP Binding via UI

Hi Folks,

Struggling with Openhab 3 HTTP binding.
Returned to OH years later (last tried early OH2). Previously I configured openhab all via files ie .things .items but decided to try UI for OH3. I like to stick to one or the other, I lose track of things otherwise.
And unfortunately the Binding page i could find didnt really comment on UI.
Seen quite a few questions with similar ish questions but not been able to translate them to my scenario. Would anyone that understands the binding maybe be able to add a translation portion to the wiki, ie here’s curl layout and how it would be input into binding for several different curl layouts.

What I’m looking to do in the HTTP binding is the same as what this curl command does:

curl -d ‘{“token”: “hiddentocken”, “message”: “Front Door open” }’ -H “Content-Type: application/json” -H “Accept: application/json” -H “OCS-APIRequest: true” -v -u “username:password” https://[Nextcloud url]/ocs/v2.php/apps/spreed/api/v1/chat/hiddentocken

Essentially end goal is when a shelly door sensor item changes state use a rule to switch a “Notification item” aka Front Door Alarm, that will send that curl command which posts a message to a group chat on nextcloud talk (a chat service).

So far this is what I’ve got. Things I’ve struggled with: 1. Getting it to work, 2. Headers specifically their layout. 3.What box is best for posting this portion of curl ‘{“token”: “hiddentocken”, “message”: “Front Door open” }’. I’ve used on/off but wasn’t sure if that would work.

label: ChatBot
thingTypeUID: http:url
configuration:
ignoreSSLErrors: false
headers:
- “Accept=application/json, OCS-APIRequest: true”
stateMethod: POST
refresh: 30
commandMethod: POST
timeout: 3000
authMode: BASIC
baseURL: https://[nextcloud URL]/ocs/v2.php/apps/spreed/api/v1/chat/hiddentocken
password: password
delay: 0
contentType: application/json
username: username
bufferSize: 2048
channels:

  • id: FrontDoorAlarm
    channelTypeUID: http:switch
    label: Front Door Alarm
    description: “”
    configuration:
    onValue: ‘{"token=“hiddentocken”, “message”=“Front Door Opened”}’
    offValue: ‘{"token=“hiddentocken”, “message”=“Front Door Closed”}’

Thanks,
Matt

If you want to add Json payload to your message and use custom headers I would go with a rule and a script action like sendHttpPostRequest(String url, String contentType, String content, Map<String, String> headers, int timeout)

Is there no way to do this with the binding?

I’ve used HTTP binding previously to read states and even change them but for some reason I cant decipher the wiki to translate my curl command into the binding.

Anyone got a similar curl command I could utilise to help translate. The niche parts of my request seem to be multiple headers, and the fact there’s JSON content I want to send back.

You should be able to add multiple headers by just creating more entries.

Anyone that stumbles upon this. I gave up with the binding. Resorted to exec binding with shell scripts running curl commands.

Wasn’t keen on going this route but just couldn’t get the HTTP binding to replicate curl command.

Sorry I missed this thread initially. I’ve got the http binding communicating with Nextcloud talk. Here’s the config of my thing and the channel I use most:

UID: http:url:ncchat
label: Nextcloud Chat
thingTypeUID: http:url
configuration:
  headers:
    - OCS-APIRequest=true
  ignoreSSLErrors: false
  stateMethod: GET
  refresh: 30
  commandMethod: POST
  timeout: 3000
  authMode: BASIC_PREEMPTIVE
  baseURL: https://[NC]/ocs/v2.php/apps/spreed/api/v1/chat
  password: ******
  delay: 0
  contentType: application/json
  username: ******
  bufferSize: 2048
channels:
  - id: Send_Message
    channelTypeUID: http:string
    label: Send Chat Message
    description: ""
    configuration:
      mode: WRITEONLY
      commandExtension: /*****
      commandTransformation: 'REGEX:s/(.*)/\{"message": "$1"\}/'

As you can see there are a couple big differences to what you had posted:

You don’t really need the Accept header, and your OCS-APIRequest header is still formatted with a : instead of the proper =. If you do need more than one header ever, then each header you add should be on a new line in the header box. Then code would look like this:

configuration:
  headers:
    - OCS-APIRequest=true
    - Accept=application/json

I think it only works if you change the basic authentication to the preemptive basic authentication

If at some point you want to get responses into OH you’ll need to set this to Get instead. Minor issue if you’re only ever sending messages.

My payload for this channel looks very different because I have it set up so that I can send any string that I set the item to, but the regular expression in my commandTransformation just puts that string into the simple {"message": "[string here]"} json. You don’t need to add the token to the payload object. Again. not a huge issue, if this has worked for you in the past then clearly the API just ignores unexpected keys in the payload.

I think that’s it. If you correct those issues I suspect that you’ll find it works.

1 Like