Is it possible to change a property over REST API?

Is it possible to change an property over REST API or do I have to use websockets?
My REST API on <my_ip>:8888 looks like this:

0:
  id: "urn:dev:ops:light_switch"
  title: "LED-Strip"
  @context: "https://iot.mozilla.org/schemas"
  properties:
    switch: 
      @type: "OnOffProperty"
      title: "my_LED-Strip"
      type: "boolean"
      description: "Whether the LED-Strip is turned on"
      links:
        0:
          rel: "property"
          href: "/1/properties/switch"

I tried with Node-Red to toggle the switch, but didn’t find a solution.

What i tried is to set the REST path to true but i am not sure if that’s the way it works:
http://<my_ip>:8888/0/properties/switch/true

That’s almost it, except that you need an API access token and you PUT the new value to /properties/switch. See https://github.com/WebThingsIO/curl-examples/blob/master/set-property.sh for an example in curl. The repo also has an example on how to get authentication, not sure how up to date that is, however. You can find the spec at https://webthings.io/api/

Thanks, looks good. But where can I get the API token from?

Edit: After a second view in to the curl-example I assume that example is used as source for target gateway because the login is for gateway.
I am looking to target the Thing directly, skipping gateway or replaceing with Node-Red.

The thing likely requires no authentication. If it does, you’ll find details for it in the thing description (top level get)

True, there is a variable called securityDefinitions that is set to nosec.

Maybe i have to go a step back and try with a more common solution.
There is an moz-extension called “rester”.
Can somebody help me work it out with that simple extension?

It’s even possible to copy the curl command in the right uper corner:

curl -X PUT 'http://<my_ip>:8888/1/properties/switch/true' \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json'


I do not know if the Authorisation Header can be skipped.
But I think so, because the curl-example readme.md says:

" Running login.sh will retrieve the JSON WebToken for the user and store it in a local file called .jwt . It will also store the URL in a local file called .url . "

I think that token is used when gateway is targeted.
If i run the curl-example login.sh i get an 404.

./login.sh
Enter      URL: <my_ip>:8888
Enter    email: <my_email>
Enter password: 
  File "<string>", line 1
    import json,sys; print(<html><title>404: Not Found</title><body>404: Not Found</body></html>['jwt'])

So it seems the REST-API of my Thing does not know what to do with a login attempt.

EDIT:
I changed the IP to <my_IP> after the request failed.
Thankfully the Response stays the same for the printscreen…

The curl for the request you’re trying to do would probably look like

 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -X PUT -d '{"switch":true}' \
 --insecure --silent \
 http://<my_ip>:8888/1/properties/switch

So you’re making a PUT request to http://<my_ip>:8888/1/properties/switch with a body of {"switch":true}.

As a FYI, here is the bash-scripted cURL command, gleaned from SetProperty.sh, that I have been using to toggle property values based on X10 button presses. Don’t forget to add a cURL timeout to account for those cases when the GW does not respond in a normal amount of time.

local xTIMEOUT=" --connect-timeout 10 --max-time 30 "

xRet=$(
    curl ${xTIMEOUT} \
    -H "Authorization:Bearer ${WTGW_AKEY}" \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \local xTIMEOUT=" --connect-timeout 10 --max-time 30 "

    -X PUT -d '{"'${_x10prop}'":'${_x10val}'}' \
    --insecure --silent --show-error \
    ${WTGW_URL}/things/${_x10wtid}/properties/${_x10prop} \
    > ${X10_MOCHAD_LOGDIR}/x10-mochad-curl-output.txt 2>&1
    _Exit=${?}
    echo "cURL exit (${_Exit})" >>${X10_MOCHAD_LOGDIR}/x10-mochad-curl-output.txt
    echo ${_Exit}
)

[quote=“freaktechnik, post:6, topic:77403, full:true”]
The curl for the request you’re trying to do would probably look like

 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -X PUT -d '{"switch":true}' \
 --insecure --silent \
 http://<my_ip>:8888/1/properties/switch

This worked in terminal. :+1:

with the MozExtension ‘RESTer’ I had to work it out.
It turns out that it does not need the --insecure --silent line at all.

curl -X PUT http://<my_ip>:8888/1/properties/switch \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -d '{"switch":false}'

This works for simple tests!

Thanks a lot!

This is the Node-Red sample flow of the above example:

[{"id":"87958e3e.960a98","type":"http request","z":"ecf5db33.a01038","name":"","method":"PUT","ret":"txt","paytoqs":"ignore","url":"http://<my_ip>:8888/1/properties/switch","tls":"","persist":false,"proxy":"","authType":"basic","x":1210,"y":760,"wires":[["96f9980.663b468"]]},{"id":"66bc516.fe65cb","type":"inject","z":"ecf5db33.a01038","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"switch\":true}","payloadType":"json","x":790,"y":740,"wires":[["dff5f673.30b9a8"]]},{"id":"96f9980.663b468","type":"debug","z":"ecf5db33.a01038","name":"Response","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1400,"y":760,"wires":[]},{"id":"676e4c1e.867cbc","type":"inject","z":"ecf5db33.a01038","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"switch\":false}","payloadType":"json","x":780,"y":780,"wires":[["dff5f673.30b9a8"]]},{"id":"dff5f673.30b9a8","type":"function","z":"ecf5db33.a01038","name":"","func":"msg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['Accept'] = 'application/json';\nmsg.body = msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1020,"y":760,"wires":[["87958e3e.960a98"]]}]