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

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.