2020-11-09 16:53:44 +00:00
# HTTP Binding
2021-04-24 18:39:51 +00:00
This binding allows using HTTP to bring external data into openHAB or execute HTTP requests on commands.
2020-11-09 16:53:44 +00:00
## Supported Things
Only one thing named `url` is available.
It can be extended with different channels.
## Thing Configuration
| parameter | optional | default | description |
|-------------------|----------|---------|-------------|
| `baseURL` | no | - | The base URL 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. |
2020-12-15 22:53:45 +00:00
| `bufferSize` | no | 2048 | The buffer size for the response data (in kB). |
2020-12-28 17:18:06 +00:00
| `delay` | no | 0 | Delay between two requests in ms (advanced parameter). |
2020-11-09 16:53:44 +00:00
| `username` | yes | - | Username for authentication (advanced parameter). |
| `password` | yes | - | Password for authentication (advanced parameter). |
2020-12-31 11:23:32 +00:00
| `authMode` | no | BASIC | Authentication mode, `BASIC` , `BASIC_PREEMPTIVE` or `DIGEST` (advanced parameter). |
2021-02-03 10:28:44 +00:00
| `stateMethod` | no | GET | Method used for requesting the state: `GET` , `PUT` , `POST` . |
| `commandMethod` | no | GET | Method used for sending commands: `GET` , `PUT` , `POST` . |
2020-11-09 16:53:44 +00:00
| `contentType` | yes | - | MIME content-type of the command requests. Only used for `PUT` and `POST` . |
2021-04-24 18:39:51 +00:00
| `encoding` | yes | - | Encoding to be used if no encoding is found in responses (advanced parameter). |
2022-05-29 06:58:56 +00:00
| `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",` . When using text based configuration include at minimum 2 headers to avoid parsing errors.|
2020-11-09 16:53:44 +00:00
| `ignoreSSLErrors` | no | false | If set to true ignores invalid SSL certificate errors. This is potentially dangerous.|
2022-12-08 20:36:05 +00:00
_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 11:23:32 +00:00
2022-12-08 20:36:05 +00:00
_Note:_ The `BASIC_PREEMPTIVE` mode adds basic authentication headers even if the server did not request authentication.
2020-12-31 11:23:32 +00: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 16:53:44 +00:00
2022-12-08 20:36:05 +00: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 17:18:06 +00:00
2022-05-29 06:58:56 +00:00
**Attention:** `baseUrl` (and `stateExtension` /`commandExtension`) should not normally use escaping (e.g. `%22` instead of `"` or `%2c` instead of `,` ).
2021-01-23 20:29:22 +00:00
URLs are properly escaped by the binding itself before the request is sent.
Using escaped strings in URL parameters may lead to problems with the formatting (see below).
2022-05-29 06:58:56 +00:00
In certain scenarios you may need to manually escape your URL, for example if you need to include an escaped `=` (`%3D`) in this scenario include `%%3D` in the URL to preserve the `%` during formatting, and set the parameter `escapedUrl` to true on the channel.
2020-11-09 16:53:44 +00:00
## Channels
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.
2022-05-29 06:58:56 +00:00
The `image` channel-type supports `stateExtension` , `stateContent` and `escapedUrl` only.
2020-11-09 16:53:44 +00:00
| 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` . |
2022-12-08 20:36:05 +00:00
| `stateTransformation` | yes | - | One or more transformation applied to received values before updating channel. |
2020-11-09 16:53:44 +00:00
| `commandTransformation` | yes | - | One or more transformation applied to channel value before sending to a remote. |
2022-05-29 06:58:56 +00:00
| `escapedUrl` | yes | - | This specifies whether the URL is already escaped. |
2021-02-03 10:28:44 +00:00
| `stateContent` | yes | - | Content for state requests (if method is `PUT` or `POST` ) |
2020-12-30 13:50:34 +00:00
| `mode` | no | `READWRITE` | Mode this channel is allowed to operate. `READONLY` means receive state, `WRITEONLY` means send commands. |
2020-11-09 16:53:44 +00:00
2021-04-24 18:39:51 +00:00
Transformations need to be specified in the same format as
2020-11-09 16:53:44 +00:00
Some channels have additional parameters.
2020-12-31 16:02:07 +00: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 16:53:44 +00: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.
Here are a few examples to unwrap an incoming value via `stateTransformation` from a complex response:
| 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::(.*?)°` |
Transformations can be chained by separating them with the mathematical intersection character "∩".
Please note that the values will be discarded if one transformation fails (e.g. REGEX did not match).
2021-04-24 18:39:51 +00:00
The same mechanism works for commands (`commandTransformation`) for outgoing values.
2020-11-09 16:53:44 +00:00
### `color`
| 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` |
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`
| parameter | optional | default | description |
|-------------------------|----------|-------------|-------------|
| `openValue` | no | - | A special value that represents `OPEN` |
| `closedValue` | no | - | A special value that represents `CLOSED` |
### `dimmer`
| 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` |
All values that are not `onValue` , `offValue` , `increaseValue` , `decreaseValue` are interpreted as brightness 0-100% and need to be numeric only.
2021-01-06 21:12:31 +00:00
### `number`
| parameter | optional | default | description |
|-------------------------|----------|-------------|-------------|
| `unit` | yes | - | The unit label for this channel |
`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 16:53:44 +00:00
### `player`
| parameter | optional | default | description |
|-------------------------|----------|-------------|-------------|
| `play` | yes | - | A special value that represents `PLAY` |
| `pause` | yes | - | A special value that represents `PAUSE` |
| `next` | yes | - | A special value that represents `NEXT` |
| `previous` | yes | - | A special value that represents `PREVIOUS` |
| `fastforward` | yes | - | A special value that represents `FASTFORWARD` |
| `rewind` | yes | - | A special value that represents `REWIND` |
### `rollershutter`
| 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` |
All values that are not `upValue` , `downValue` , `stopValue` , `moveValue` are interpreted as position 0-100% and need to be numeric only.
2021-04-24 18:39:51 +00:00
2020-11-09 16:53:44 +00:00
### `switch`
| parameter | optional | default | description |
|-------------------------|----------|-------------|-------------|
| `onValue` | no | - | A special value that represents `ON` |
| `offValue` | no | - | A special value that represents `OFF` |
**Note:** Special values need to be exact matches, i.e. no leading or trailing characters and comparison is case-sensitive.
## URL Formatting
2021-04-24 18:39:51 +00: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/javase/6/docs/api/java/util/Formatter.html ).
2020-11-09 16:53:44 +00: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 10:28:44 +00: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 16:53:44 +00:00
When sending an OFF command on 2020-07-06, the URL
2022-12-08 20:36:05 +00:00
```text
2020-11-09 16:53:44 +00:00
http://www.domain.org/home/lights/23871/?status=%2$s& date=%1$tY-%1$tm-%1$td
2021-04-24 18:39:51 +00:00
```
2020-11-09 16:53:44 +00:00
2021-04-24 18:39:51 +00:00
is transformed to
2020-11-09 16:53:44 +00:00
2022-12-08 20:36:05 +00:00
```text
2020-11-09 16:53:44 +00:00
http://www.domain.org/home/lights/23871/?status=OFF& date=2020-07-06
2020-12-30 13:50:34 +00:00
```
2021-02-14 03:44:34 +00:00
## Examples
### `demo.things`
2022-12-08 20:36:05 +00:00
```java
2021-04-24 18:39:51 +00:00
Thing http:url:foo "Foo" [
2022-12-18 09:22:07 +00: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" ]
2021-02-14 03:44:34 +00:00
}
```