2020-11-09 17:53:44 +01:00
# HTTP Binding
2021-04-24 20:39:51 +02:00
This binding allows using HTTP to bring external data into openHAB or execute HTTP requests on commands.
2020-11-09 17:53:44 +01:00
## Supported Things
Only one thing named `url` is available.
It can be extended with different channels.
## Thing Configuration
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|-----------------------|----------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `baseURL` | no | - | The base URL (including protocol `http://` or `https://` ) for this thing. Can be extended in channel-configuration. |
| `refresh` | no | 30 | Time in seconds between two refresh calls for the channels of this thing. |
| `timeout` | no | 3000 | Timeout for HTTP requests in ms. |
| `bufferSize` | no | 2048 | The buffer size for the response data (in kB). |
| `delay` | no | 0 | Delay between two requests in ms (advanced parameter). |
| `username` | yes | - | Username for authentication (advanced parameter). |
| `password` | yes | - | Password for authentication (advanced parameter). Also used for the authentication token when using `TOKEN` authentication. |
| `authMode` | no | BASIC | Authentication mode, `BASIC` , `BASIC_PREEMPTIVE` , `TOKEN` or `DIGEST` (advanced parameter). |
| `stateMethod` | no | GET | Method used for requesting the state: `GET` , `PUT` , `POST` . |
| `commandMethod` | no | GET | Method used for sending commands: `GET` , `PUT` , `POST` . |
| `contentType` | yes | - | MIME content-type of the command requests. Only used for `PUT` and `POST` . |
2024-08-20 17:02:43 +02:00
| `encoding` | yes | - | Encoding to be used if no encoding is found in responses (advanced parameter). |
| `headers` | yes | - | Additional headers that are sent along with the request. Format is "header=value". Multiple values can be stored as `headers="key1=value1", "key2=value2", "key3=value3",` |
2024-01-14 22:08:11 +01:00
| `ignoreSSLErrors` | no | false | If set to true, ignores invalid SSL certificate errors. This is potentially dangerous. |
| `strictErrorHandling` | no | false | If set to true, thing status is changed depending on last request result (failed = `OFFLINE` ). Failed requests result in `UNDEF` for channel values. |
| `userAgent` | yes | (yes ) | Sets a custom user agent (default is "Jetty/version", e.g. "Jetty/9.4.20.v20190813"). |
_Note:_ Optional "no" means that you have to configure a value unless a default is provided, and you are ok with that setting.
2020-12-31 12:23:32 +01:00
2022-12-08 21:36:05 +01:00
_Note:_ The `BASIC_PREEMPTIVE` mode adds basic authentication headers even if the server did not request authentication.
2020-12-31 12:23:32 +01:00
This is dangerous and might be misused.
The option exists to be able to authenticate when the server is not sending the proper 401/Unauthorized code.
Authentication might fail if redirections are involved as headers are stripper prior to redirection.
2020-11-09 17:53:44 +01:00
2022-12-08 21:36:05 +01:00
_Note:_ If you rate-limit requests by using the `delay` parameter you have to make sure that the time between two refreshes is larger than the time needed for one refresh cycle.
2020-12-28 18:18:06 +01:00
2024-07-24 12:20:45 +02:00
**Attention:** `baseUrl` (and `stateExtension` /`commandExtension`) don't normally require percent encoding (e.g. `%22` instead of `"` or `%2C` instead of `,` ).
2021-01-23 21:29:22 +01:00
URLs are properly escaped by the binding itself before the request is sent.
2024-07-24 12:20:45 +02:00
When automatic encoding is not possible (e.g. because you need to include an encoded `=` or `&` in the query string) you can use manual encoding with a doubled `%` (`%%3D` instead of `=` ).
2021-01-23 21:29:22 +01:00
2020-11-09 17:53:44 +01:00
## Channels
2024-01-14 22:08:11 +01:00
The thing has two channels of type `request-date-time` which provide the timestamp of the last successful (`last-success`) and last failed (`last-failure`) request.
Additionally, the thing can be extended with data channels.
2020-11-09 17:53:44 +01:00
Each item type has its own channel-type.
Depending on the channel-type, channels have different configuration options.
All channel-types (except `image` ) have `stateExtension` , `commandExtension` , `stateTransformation` , `commandTransformation` and `mode` parameters.
2024-01-14 22:08:11 +01:00
The `image` channel-type supports `stateExtension` only.
| parameter | optional | default | description |
|-------------------------|----------|-------------|-----------------------------------------------------------------------------------------------------------|
| `stateExtension` | yes | - | Appended to the `baseURL` for requesting states. |
| `commandExtension` | yes | - | Appended to the `baseURL` for sending commands. If empty, same as `stateExtension` . |
2024-08-20 17:02:43 +02:00
| `stateTransformation` | yes | - | One or more transformation applied to received values before updating channel. |
2024-01-14 22:08:11 +01:00
| `commandTransformation` | yes | - | One or more transformation applied to channel value before sending to a remote. |
| `stateContent` | yes | - | Content for state requests (if method is `PUT` or `POST` ) |
2020-12-30 14:50:34 +01:00
| `mode` | no | `READWRITE` | Mode this channel is allowed to operate. `READONLY` means receive state, `WRITEONLY` means send commands. |
2020-11-09 17:53:44 +01:00
Some channels have additional parameters.
2020-12-31 17:02:07 +01:00
When concatenating the `baseURL` and `stateExtension` or `commandExtension` the binding checks if a proper URL part separator (`/`, `&` or `?` ) is present and adds a `/` if missing.
2020-11-09 17:53:44 +01:00
### Value Transformations (`stateTransformation`, `commandTransformation`)
Transformations can be used if the supplied value (or the required value) is different from what openHAB internal types require.
2024-03-02 12:01:24 +01:00
The relevant transformation service needs to be installed via the Main UI or addons.cfg before use.
2020-11-09 17:53:44 +01:00
Here are a few examples to unwrap an incoming value via `stateTransformation` from a complex response:
2024-08-20 17:02:43 +02:00
| Received value | Tr. Service | Transformation |
|---------------------------------------------------------------------|-------------|--------------------------------------------|
| `{device: {status: { temperature: 23.2 }}}` | JSONPATH | `JSONPATH($.device.status.temperature)` |
| `<device><status><temperature>23.2</temperature></status></device>` | XPath | `XPath(/device/status/temperature/text())` |
| `THEVALUE:23.2°C` | REGEX | `REGEX(:(.*?)°)` |
2020-11-09 17:53:44 +01:00
2024-08-20 17:02:43 +02:00
Transformations can be chained in the UI by listing each transformation on a separate line, or by separating them with the mathematical intersection character "∩".
Transformations are defined using this syntax: `TYPE(FUNCTION)` , e.g.: `JSONPATH($.path)` .
The syntax: `TYPE:FUNCTION` is still supported, e.g.: `JSONPATH:$.path` .
2020-11-09 17:53:44 +01:00
Please note that the values will be discarded if one transformation fails (e.g. REGEX did not match).
2021-04-24 20:39:51 +02:00
The same mechanism works for commands (`commandTransformation`) for outgoing values.
2020-11-09 17:53:44 +01:00
### `color`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|-----------------|----------|---------|---------------------------------------------------------------------------|
| `onValue` | yes | - | A special value that represents `ON` |
| `offValue` | yes | - | A special value that represents `OFF` |
| `increaseValue` | yes | - | A special value that represents `INCREASE` |
| `decreaseValue` | yes | - | A special value that represents `DECREASE` |
| `step` | no | 1 | The amount the brightness is increased/decreased on `INCREASE` /`DECREASE` |
| `colorMode` | no | RGB | Mode for color values: `RGB` or `HSB` |
2020-11-09 17:53:44 +01:00
All values that are not `onValue` , `offValue` , `increaseValue` , `decreaseValue` are interpreted as color value (according to the color mode) in the format `r,g,b` or `h,s,v` .
### `contact`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|---------------|----------|---------|------------------------------------------|
| `openValue` | no | - | A special value that represents `OPEN` |
| `closedValue` | no | - | A special value that represents `CLOSED` |
2020-11-09 17:53:44 +01:00
### `dimmer`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|-----------------|----------|---------|---------------------------------------------------------------------------|
| `onValue` | yes | - | A special value that represents `ON` |
| `offValue` | yes | - | A special value that represents `OFF` |
| `increaseValue` | yes | - | A special value that represents `INCREASE` |
| `decreaseValue` | yes | - | A special value that represents `DECREASE` |
| `step` | no | 1 | The amount the brightness is increased/decreased on `INCREASE` /`DECREASE` |
2020-11-09 17:53:44 +01:00
All values that are not `onValue` , `offValue` , `increaseValue` , `decreaseValue` are interpreted as brightness 0-100% and need to be numeric only.
2021-01-06 22:12:31 +01:00
### `number`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|-----------|----------|---------|---------------------------------|
| `unit` | yes | - | The unit label for this channel |
2021-01-06 22:12:31 +01:00
`number` channels can be used for `DecimalType` or `QuantityType` values.
If a unit is given in the `unit` parameter, the binding tries to create a `QuantityType` state before updating the channel, if no unit is present, it creates a `DecimalType` .
Please note that incompatible units (e.g. `°C` for a `Number:Density` item) will fail silently, i.e. no error message is logged even if the state update fails.
2020-11-09 17:53:44 +01:00
### `player`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|--------------------|----------|---------|-----------------------------------------------|
| `playValue` | yes | - | A special value that represents `PLAY` |
| `pauseValue` | yes | - | A special value that represents `PAUSE` |
| `nextValue` | yes | - | A special value that represents `NEXT` |
| `previousValue` | yes | - | A special value that represents `PREVIOUS` |
| `fastforwardValue` | yes | - | A special value that represents `FASTFORWARD` |
| `rewindValue` | yes | - | A special value that represents `REWIND` |
2020-11-09 17:53:44 +01:00
### `rollershutter`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|-------------|----------|---------|----------------------------------------|
| `upValue` | yes | - | A special value that represents `UP` |
| `downValue` | yes | - | A special value that represents `DOWN` |
| `stopValue` | yes | - | A special value that represents `STOP` |
| `moveValue` | yes | - | A special value that represents `MOVE` |
2020-11-09 17:53:44 +01:00
All values that are not `upValue` , `downValue` , `stopValue` , `moveValue` are interpreted as position 0-100% and need to be numeric only.
2024-08-20 17:02:43 +02:00
2020-11-09 17:53:44 +01:00
### `switch`
2024-01-14 22:08:11 +01:00
| parameter | optional | default | description |
|------------|----------|---------|---------------------------------------|
| `onValue` | no | - | A special value that represents `ON` |
| `offValue` | no | - | A special value that represents `OFF` |
2020-11-09 17:53:44 +01:00
**Note:** Special values need to be exact matches, i.e. no leading or trailing characters and comparison is case-sensitive.
## URL Formatting
2024-12-21 08:20:08 +01:00
After concatenation of the `baseURL` and the `commandExtension` or the `stateExtension` (if provided) the URL is formatted using the [java.util.Formatter ](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Formatter.html ).
2020-11-09 17:53:44 +01:00
The URL is used as format string and two parameters are added:
- the current date (referenced as `%1$` )
- the transformed command (referenced as `%2$` )
After the parameter reference the format needs to be appended.
2021-02-03 11:28:44 +01:00
See the link above for more information about the available format parameters (e.g. to use the string representation, you need to append `s` to the reference, for a timestamp `t` ).
2020-11-09 17:53:44 +01:00
When sending an OFF command on 2020-07-06, the URL
2024-08-20 17:02:43 +02:00
```text
2024-01-14 22:08:11 +01:00
http://www.domain.org/home/lights/23871/?status=%2$s& date=%1$tY-%1$tm-%1$td
2024-08-20 17:02:43 +02:00
```
2020-11-09 17:53:44 +01:00
2021-04-24 20:39:51 +02:00
is transformed to
2020-11-09 17:53:44 +01:00
2024-08-20 17:02:43 +02:00
```text
2020-11-09 17:53:44 +01:00
http://www.domain.org/home/lights/23871/?status=OFF& date=2020-07-06
2020-12-30 14:50:34 +01:00
```
2021-02-14 04:44:34 +01:00
## Examples
### `demo.things`
2022-12-08 21:36:05 +01:00
```java
2021-04-24 20:39:51 +02:00
Thing http:url:foo "Foo" [
2024-08-20 17:02:43 +02:00
baseURL="https://example.com/api/v1/metadata-api/web/metadata",
headers="key1=value1", "key2=value2", "key3=value3",
refresh=15] {
Channels:
Type string : text "Text" [ stateTransformation="JSONPATH($.metadata.data)" ]
}
```
Multiple transformations can be chained in the .things file by listing them as comma-separated values:
```java
Thing http:url:foo "Foo" [
baseURL="https://example.com/api/v1/metadata-api/web/metadata",
headers="key1=value1", "key2=value2", "key3=value3",
refresh=15] {
Channels:
Type string : text "Text" [ stateTransformation="REGEX(.*meta.*)", "JSONPATH($.metadata.data)" ]
}
```
or by combining them into one value, separated by "∩":
```java
Thing http:url:foo "Foo" [
baseURL="https://example.com/api/v1/metadata-api/web/metadata",
2022-12-18 10:22:07 +01:00
headers="key1=value1", "key2=value2", "key3=value3",
refresh=15] {
Channels:
2024-08-20 17:02:43 +02:00
Type string : text "Text" [ stateTransformation="REGEX(.*meta.*) ∩ JSONPATH($.metadata.data)" ]
2021-02-14 04:44:34 +01:00
}
```