[HTTP Binding] Using PUT Methods

Hi,

i am planing to integrate my hue motion detectors into my running openhab installation, cause I don’t want to use serveral apps.

To integrate the futher features of Hue, the existing addon isn’t providing, i need a way to talk to the hue api.

I already integrated the sensors (motion, light, temp, etc…) and because i dont realy like the hardcoded way for creating light scenes in openhab, i want to use my existing hue scenes (espacialy bacause my girlfriend want to change the scenes on her own).

There is a way to read and controll all of them via the hue api. Here the example for the state of the motionsensor or activating a scene for a group:
http://{{ip}}/api/{{username}}/sensors/{{sensor_id}}/config PUT:{“on”:true}
http://{{ip}}/api/{{username}}/groups/{{group_id}}/action PUT:{“scene”:“scene_id”}

Is there a way to use the http Binding with the PUT statement? In the documantation is just the POST statement mentioned.

Felix

I do not have an answer to your question, but have you considered using executeCommandLine to call a script with a CURL command?

Good idea, sometimes i cannot see the wood for the trees :smiley:

Just wrote a qick’n dirtya one for testing:

#!/bin/sh

case $1 in
  1)
    curl -X PUT -H "Content-Type: application/json" -d '{"scene":"{{scene_id1}}"}' "http://{{ip}}/api/{{username}}/groups/{{groupip}}/action";;
  2)
    curl -X PUT -H "Content-Type: application/json" -d '{"scene":"{{scene_id2}}"}' "http://{{ip}}/api/{{username}}/groups/{{groupip}}/action";;
esac

Thanks for the hint.

Thanks for posting this.

It put in me in the right direction for POSTing commands to a PixOut Art-net player.

It’s not the elegant Switch item I wanted, but with a little rule and virtual switch, it seems to be working.

# Usage :- cue_action.sh --cue=12345 --action=play/pause/stop --int=50 --url=192.168.1.230



while [ "$#" -gt 0 ]; do
  case "$1" in
    -c) cue="$2"; shift 2;;
    -i) int="$2"; shift 2;;
	-u) url="$2"; shift 2;;
    -a) action="$2"; shift 2;;
    --cue=*) cue="${1#*=}"; shift 1;;
    --int=*) int="${1#*=}"; shift 1;;
	--url=*) url="${1#*=}"; shift 1;;
    --action=*) action="${1#*=}"; shift 1;;
    --cue|--lumin|--action) echo "$1 requires an argument"
>&2; exit 1;;
    -*) echo "unknown option: $1" >&2; exit 1;;
    *) handle_argument "$1"; shift 1;;
  esac

echo $cue
echo $int
echo $action
echo $url
echo $(date +%F" "%T)

curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" "http://$url/px/v1/player/pl/$cue/$action"
curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" "http://$url/px/v1/player/pl/$cue/setIntensity/$int"


done

Does anybody know how to use the HTTP Binding using the PUT method?
I’d like to configure a thing with appropriate channels to control iTunes (now Music) app.
github - itunes-api
The HTTP GET part is working well, so I can display now playing info.

I’m also aware of the OH-iTunes-API integration github project which is mainly based on shell scripts, but I want to learn the HTTP way.

The following shell command works, but I don’t know how to do it using the HTTP binding:
> curl -d 'level=10' -X PUT http://host:port/volume
I couldn’t find any info on how to set up the channel parameters, how and where the volume state should be added to be sent out in the request.

Thank you in advance

I made some progress by setting the channel’s command transformation to this:
REGEX:s/(.*)/level=$1/

As I set the HTTP binding log level to TRACE, I can see the command sent out:
Sending to 'http://<host>:<port>/volume': Method = {PUT}, Headers = {Accept-Encoding: gzip, User-Agent: Jetty/9.4.20.v20190813}, Content = {level=46}

Unfortunately the command has no effect on the volume

Now it finally works after hours of debugging and trial-errors! :partying_face:
I had to add some debug info in itunes-api’s app.js to see the volume request that is received.
The culprit was the content type, which I could identify from the request received by the api. It turned out that curl set it to application/x-www-form-urlencoded, and none of the HTTP Binding’s preset values could produce the same result.

I think this was a hard one to understand and configure, maybe some hints or examples should be added to the docs. For example the HTTP Binding doc’s value transformation part could present some command transformations, now there’s only state transformation examples…

So here’s the solution summary (at least for itunes-api)
Thing - Advanced Configuration:

  • Command Method: PUT
  • Content Type: don’t select any of these!
  • Headers: Content-Type=application/x-www-form-urlencoded

Volume Channel - Configuration: (Channel type: Dimmer)

  • Command URL Extension: volume
  • Command Transformation: REGEX:s/(.*)/level=$1/

Cheers

System: macOS 10.15
Music (iTunes): 1.0.6.10
openHAB: 3.0.0 release
Binding: HTTP v3.0.0