I was able to successfully send a command to the REST API using Siri Shortcuts whilst outside my LAN.
I knew it was possible to do it from within the LAN but it’s taken hours to figure out how to do it from outside the LAN (Google was very much my friend here). In short, the key was to encode the credentials using Base64 and to not escape the @ symbol in the username. Only figured it out when I compared the output of a cURL to what I was generating using the Shortcuts app.
Below is a step-by-step guide as well as some screenshots. I couldn’t work out how to publish a sample shortcut without removing my credentials, so hopefully this guide will be enough to get others going.
Add a “Text” action. In the action enter the command you wish to send: ie OFF
Add another “Text” action. In this action, enter your credentials for myopenhab.org as such: username:password . If your username contains any symbols, eg the ‘@’ symbol, do not escape these.
Add a “Base64 Encode” action. This should be after the “Text” action which contains the credentials. A line should connect the “Text” and the “Base64 Encode” actions. I selected “None” in Line Breaks (this can’t be seen in the screenshot) but I don’t think it matters.
Add a “URL” action. In this action, add the url to your item’s REST endpoint ie https://myopenhab.org/rest/items/item_to_switch
After the “URL” action, add a “Get Contents of URL” action. This action contains a few entries which require configuring.
Change the Method to POST and add the following 3 headers:
- Content-Type: text/plain
- Accept: application/json
- Authorization: Basic
In the Authorization header, after the word Basic, insert a link to the “Base64 Encode” action (check out the screenshot). A space is required between the word Basic and the “Base64 Encode” action. eg: Basic Base64 Encode
Finally select File in the Request Body section and insert the Text action which contains the command you wish to send to the item.
After this press the play button. A successful response will (other than switching your item) result in a blank page in the quick view under the final step.
Hopefully this is useful to others