REST Switch help

Hi All!

Can’t figure out. Why first curl switch works perfect and second rest not?

switch:
  - platform: command_line
    switches:
      loved_track:
        command_on: "/usr/bin/curl -X PUT -d 'loved=true' http://itunes.api.ip:8181/loved_track"
        command_off: "/usr/bin/curl -X PUT -d 'loved=false' http://itunes.api.ip:8181/loved_track"
        command_state: "/usr/bin/curl -k --silent http://itunes.api.ip:8181/loved_track"
        command_timeout: 10
        value_template: '{{ value_json.loved }}'
        friendly_name: Loved Track
  - platform: rest
    resource: http://itunes.api.ip:8181/loved_track
    method: put
    name: Loved Track Rest
    body_on: 'loved=true'
    body_off: 'loved=false'
    is_on_template: '{{ value_json.loved }}'

On REST API side I may see request body when curl put request come. And empty request body when rest switch request come.
Am I missing something?

Are you using https://github.com/maddox/itunes-api as the API server?
If so, I can’t find the loved_track endpoint :thinking:

Yes. I’m working on some useful features like - search, mark tracks as loved. I’ll think about Pull requests to the @maddox repository.

Ah, I was wondering.
Reading about the upstream library, shouldn’t JSON work here as well?

  - platform: rest
    resource: http://itunes.api.ip:8181/loved_track
    method: put
    name: Loved Track Rest
    body_on: '{"loved":"true"}'
    body_off: '{"loved":"false"}'
    headers:
      Content-Type: application/json
    is_on_template: '{{ value_json.loved }}'

For me it’s like hobby. I’m not expert in Javascript and especially AppleScript :grin:

About JSON - Nope.
Like shuffle, volume, etc - it’s simple variable=value in the body.

Well, look at that, there’s actually an example for your use-case in the RESTful Command Integration :laughing:
So if you can’t get the switch working, you could at least try setting up a command, however I don’t think that sould give a different result :slight_smile:
So this should work I guess:

  - platform: rest
    resource: http://itunes.api.ip:8181/loved_track
    method: put
    name: Loved Track Rest
    body_on: 'loved=true'
    body_off: 'loved=false'
    content_type: "application/x-www-form-urlencoded"
    is_on_template: '{{ value_json.loved }}'

Thank you! You are right. Must specify right content-type in header! Final version is:

  - platform: rest
    resource: http://itunes.api.ip:8181/loved_track
    method: put
    name: Loved Track Rest
    body_on: 'loved=true'
    body_off: 'loved=false'
    headers:
      Content-Type: application/x-www-form-urlencoded
    is_on_template: '{{ value_json.loved }}'

PS: About additional itunes-api endpoints. It would be great to integrate itunes-api and Media Browser. It doesn’t seem as impossible as how it looks first :grin: But a lot of work here.

Hello,
Anyone knows why I’m get command failed?
thanks

switch:
  - platform: command_line
    switches:
      cam_motion:
        command_on: "/usr/bin/curl -u user:pass -X PUT -d '<PrivacyMask><enabled>true</enabled><normalizedScreenSize><normalizedScreenWidth>704</normalizedScreenWidth><normalizedScreenHeight>576</normalizedScreenHeight></normalizedScreenSize><PrivacyMaskRegionList><PrivacyMaskRegion><id>1</id><enabled>true</enabled><RegionCoordinatesList><RegionCoordinates><positionX>9</positionX><positionY>124</positionY></RegionCoordinates><RegionCoordinates><positionX>698</positionX><positionY>124</positionY></RegionCoordinates><RegionCoordinates><positionX>698</positionX><positionY>558</positionY></RegionCoordinates><RegionCoordinates><positionX>9</positionX><positionY>558</positionY></RegionCoordinates></RegionCoordinatesList></PrivacyMaskRegion></PrivacyMaskRegionList></PrivacyMask>' --digest http://192.168.X.X/ISAPI/System/Video/inputs/channels/1/privacyMask"