[webhook] New, very simple binding for listening incomming http requests

Thank you
In my scenario (OH4) Binding build and deploy succeed but the curl request show this log message:
image

I would need to capture a call of this type:
curl -X POST 'http://localhost:8080/webhook/thingName' -H 'content-type: application/x-www-form-urlencoded' -d '{"content":"blablablabla"}'
… and send content (json payload, in this example) to an Item
Is it possible ?

If your device is able to send http/postvrequests, did you check, if you can directly send it to openhabā€˜s rest API?

Post is supported by this binding. Not sure how it needs to be configured. Could you try the following channel config

  - id: PostTest
    channelTypeUID: webhook:string-channel
    label: Test for Post Request
    description: ""
    configuration:
      expression: req.parameters.content[0];

Probably this wonā€˜t work… In this case try

    expression: req.body.text

or

    expression: req.body.json

I see that your binding supports jexl expression language. Just in case to avoid work on your side: channels do now support script transformations.

Adding a script in one of four already supported script languages to a channel might be better than to add a script in an extra language? I also read in one of your posts that you are thinking of adding js instead of jexl…

It’s not possibile, because my citophone cannot build an ā€œapplication/jsonā€ content-type as required by rest api (it can only build ā€œapplication/x-www-form-urlencodedā€ content-type)

@Nicola_Cisternino Support body application/x-www-form-urlencoded Ā· Issue #1 Ā· ptrbojko/openhab-webhook-binding Ā· GitHub
@Oliver2 Remove JEXL in fabours of built in scripting solutions Ā· Issue #2 Ā· ptrbojko/openhab-webhook-binding Ā· GitHub

Also, @Oliver2 thank you for your donation!

I wouldn’t necessarily remove it as some users might already have made use of it - I just wanted to point you in that direction in case you wanted to start a project of supporting JS in your binding

I’ve patched a thing or two here and there and checked against OH 4.0.4.

https://github.com/ptrbojko/openhab-webhook-binding/actions/runs/7254341980/artifacts/1122609589

Having this fairly stable I will push it to Marketplace.

Works like a charm.
One minor thing: After having created a channel if you configure the channel later on, there is a dropdown field missing to change channelTypeUID. Otherwise no problem as you can always directly change it in the code tab.

Please verify features I have in my mind as roadmap:

  1. Support for built-in transformations - this is somehow limited because you can only pass a string to transformation scripts, so some intermediate JSON should be constructed . However this would be more native to openhab - not to use yet another scripting language like JEXL.
  2. Alternatively change JEXL to spring baked SpEL or leave it as it is. Changing back and forth to string would influence performance and memory handling for some heavy users.

This binding is similar in some way to MQTT binding. In MQTT binding you only have support for builtin transformations and its working, though there are string like values by design. Pitty, that builtin transformations only supports passing a string values.

  1. Support for POST of url-encoded-forms
  2. Support for uploading files through http POST method. Basic use case would be for images uploaded to openhab, or post of json/xmls
  3. Support for FTP in some way (like upload of files to openhab) - some old cameras may upload an image this way, or some may upload a screenshot with a script.

Any of this is not promised. I maintain two bindings and I am limited by frames of ordinary life :slight_smile:

Currently your binding provides to me everything I need.

My 2 cents:

  1. and 2.:
    Openhab itself already supports various transformations at thing-channel level like
  • JSONPATH
  • MAP
  • SCRIPT ECMAScript (ECMAScript 262 Edition 11)
  • SCRIPT Rule DSL (v1)
  • REGEX
    etc

A built in transformation or script support is nice, but not required as standard mechanisms are already in place. Iā€˜d spend that time for development to make your binding an official binding for openhab.
To me, your binding is a very important universal ā€žswiss knifeā€œ for IoT devices where there is no proper binding available.

5 Likes

Binding is delivered to Third party marketplace

3 Likes

Hi,

It works with application/x-www-form-urlencoded.

Example thing:

UID: webhook:Webhook:aaa
label: Webhook thing
thingTypeUID: webhook:Webhook
configuration:
  expression: resp.status=200
channels:
  - id: a1
    channelTypeUID: webhook:string-channel
    label: a1 label
    description: ""
    configuration:
      expression: return req.parameters.action[0];

Call:

curl -X POST "http://localhost:8080/webhook/aaa" -H "Content-Type: application/x-www-form-urlencoded" -d "action=somevalue"
1 Like

Great job Piotr! :+1:
Thank you for your contribution

3 Likes

It works !!
Thanks Piotr !

… in your example, the working url should be:
curl -X POST "http://localhost:8080/webhook/aaa/a1" -H "Content-Type: application/x-www-form-urlencoded" -d "action=somevalue"
? (including channel id)

Could somebody post a simple but complete example how to use this?

I tried to change the state of an item, but i cant see any changes.

Here you go:

UID: webhook:Webhook:CMD
label: Webhook CMD
thingTypeUID: webhook:Webhook
configuration:
  expression: resp.status=200
channels:
  - id: lastCall
    channelTypeUID: webhook:lastCall-channel
    label: Last request
    description: Timestamp of the last request to the webhook.
    configuration: {}
  - id: myItem1
    channelTypeUID: webhook:trigger-channel
    label: myItem1
    description: ""
    configuration:
      expression: req.parameters.myItem1[0];
  - id: myItem2
    channelTypeUID: webhook:switch-channel
    label: myItem2
    description: ""
    configuration:
      expression: req.parameters.myItem2[0];

In a browser simply run:
http://openhab:8080/webhook/CMD?myItem1=whatever
Here you do not need an item - just a rule which gets triggered by channel webhook:Webhook:CMD:myItem1

http://openhab:8080/webhook/CMD?myItem2=ON
Here you need to link a switch item to the channel webhook:Webhook:CMD:myItem2

2 Likes

Thanks, @Oliver2 !

I still dont get any response from openhab. shouldn’t I see events in the eventlog?

I used the second example and linked it with a light, neither ON or OFF shows a response.