HTTP binding send JSON body in PUT request (restful api)

I’m trying to configure a switch on a Hue behaviour. Reading works, writing does not. I tried the documentation, but even as a software engineer, I do not understand how this is supposed to work.

I simply need it to post {"enabled": true} or {"enabled": false} depending on the switch state. There’s a only a single example in the docs for dynamic urls, but that’s a GET edge case. Can somebody please provide an example on how to work a REST API?

UID: http:url:sz_wakeup_light
label: HTTP URL Thing
thingTypeUID: http:url
configuration:
  authMode: BASIC
  headers:
    - hue-application-key=<hidden>
  ignoreSSLErrors: true
  baseURL: https://<hidden>/clip/v2/resource/behavior_instance/adc2736e-f3fd-465c-8206-f061ae50115c
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: PUT
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: enabled
    channelTypeUID: http:switch
    label: enabled
    description: ""
    configuration:
      onValue: "true"
      stateContent: '{"enabled": %2$}'
      offValue: "false"
      stateTransformation: JSONPATH:$.data[0].enabled

Toggling a switch on this thing leads to an internal object reference to be posted. Is this a bug or a missing configuration? I cannot see which configuration item might produce this.

Requesting 'https://<hidden>/clip/v2/resource/behavior_instance/adc2736e-f3fd-465c-8206-f061ae50115c' (method='PUT', content='org.eclipse.jetty.client.util.StringContentProvider@1acd7f1') failed: 400 Bad Request

you want to have a commandTransformation

Maybe try

commandTransformation: JSONPATH:$.enabled

JSONPATH extracts data from JSON, it does not hav a reverse function.

I think you’re fighting the onValue/offValue, these are bi-directional and apply to outbound commands too.

There is an enhancement request in for a formatBeforePublish parameter, akin to the one in the MQTT binding. That would do the trick, if implemented.

IMO this should be done by a proper transformation. If the required format is more complex than the one presented above, it’s not so easy to put it in one input field.

Thanks. So the stateContent is only used to build a request body in the Pull-Case, but commandTransformation forms the body in the reverse direction.
I put in a jinja transformation JINJA:{"enabled": {{value}} }. This works for my case.

The Jinja transformation has value_json for json-structured values. Leaves the question how more complex states would be represented (e.g. brightness, color) towards the transformation plugin.

This part should be documented both better and with a more complex example.

Oh, this is interesting: could you share your updated full Thing YAML?

1 Like
UID: http:url:wakeup_light
label: HTTP URL Thing
thingTypeUID: http:url
configuration:
  authMode: BASIC
  headers:
    - hue-application-key=<hidden>
  ignoreSSLErrors: true
  baseURL: https://<hidden>/clip/v2/resource/behavior_instance/adc2736e-f3fd-465c-8206-f061ae50115c
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: PUT
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: enabled
    channelTypeUID: http:switch
    label: enabled
    description: ""
    configuration:
      onValue: "true"
      commandTransformation: 'JINJA:{"enabled": {{value}} }'
      offValue: "false"
      stateTransformation: JSONPATH:$.data[0].enabled

There is no authentication since this is using a preshared key, disabling authMode is not available but basic with no username/password works.

The uuid of the behaviour needs to be looked up through https://<hidden>/clip/v2/resource/behavior_instance/.

Just a warning. The OpenHab Jinja transformation “JinJava” seems to be very limited, if not crippled. Most functions or even string formatting doesn’t work. So while Jinja would be a good and powerful tool for this kind of problem, the Jinja transformation seems not to be.
The Javascript transformation is working for other cases but you have to create a file per snippet and plain Javascript is very inefficient to write.

I have a need to POST a string from a String Item, but embedded within some JSON. I’m currently doing this with a proxy Item and a rule, but am looking to get rid of these extras. This JINJA transformation might be what I need, so thanks for sharing!

I’m trying to avoid JS transformation as you can’t define this within the UI, as you say.

EDIT: Well, that worked a treat! Thanks again for sharing this - I’ve put in a PR to the JINJA documentation to give an outgoing transformation example.

1 Like

Hello, thanks for sharing this… Do you think it will work for a JSON like this (for an elgato light) ?

{
    "numberOfLights": 1,
    "lights": [
        {
            "on": 1,
            "brightness": 20,
            "temperature": 200
        }
    ]
}```

Well, it seems it does work, i can turn on and off the elgato light… Now I need to refine a bit the state side of the story.