I'm working on a Skin that is accessing data from a very basic (home grown) home automation system.
One of the things I want to do is have a button that when clicked will turn a light on or off.
I already have a measure that gets the current state of the light (HTTP GET from a REST API), but I'm struggling with how to do an HTTP POST or PUT to change the state of the light.
Possible approaches I have considered:
1) Using WebParser - problem is I can't see any way to set the request method to POST
2) Lua script - according to the docs 'require' is not supported, so I don't know how to get http support in Lua
3) Custom plugin - I hoped there might already be one
I'm not afraid of writing a bit of C++ if that's what it takes, but I'd prefer not to.
Any suggestions or hints would be appreciated.
It is currently April 20th, 2024, 8:55 am
How to HTTP POST from an action?
-
- Posts: 3
- Joined: May 5th, 2020, 8:20 am
-
- Rainmeter Sage
- Posts: 16148
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: How to HTTP POST from an action?
Can you do this from your browser, via command line or anyway else?alistair@fred wrote: ↑May 5th, 2020, 8:36 am I'm struggling with how to do an HTTP POST or PUT to change the state of the light.
-
- Rainmeter Sage
- Posts: 2604
- Joined: March 23rd, 2015, 5:26 pm
Re: How to HTTP POST from an action?
WebParser is GET-only. As mentioned above, you can use curl from a PowerShell command line (using a CommandMeasure).
-
- Posts: 3
- Joined: May 5th, 2020, 8:20 am
Re: How to HTTP POST from an action?
Thanks for the suggestions.
I thought about using curl after I'd posted my query last night. I'm at work at the moment, so will give it a try tonight...
I thought about using curl after I'd posted my query last night. I'm at work at the moment, so will give it a try tonight...
-
- Posts: 3
- Joined: May 5th, 2020, 8:20 am
Re: How to HTTP POST from an action?
I've managed to do this using curl, as suggested.
The measure that does the work looks like this:
The toggleLights variable gets set to true or false elsewhere. Note the escaping needed on the quotes to keep curl happy...
I hope this may be useful to someone else.
Alistair.
The measure that does the work looks like this:
Code: Select all
[measureToggleLights]
Measure=Plugin
Plugin=RunCommand
Parameter=curl --header "Content-Type: application/json" --request POST --data "{\"lightsOn\":#toggleLights#,\"timestamp\":0,\"id\":0}" http://192.168.2.211:34568/api/v1/lights
State=Show
DynamicVariables=1
I hope this may be useful to someone else.
Alistair.
-
- Rainmeter Sage
- Posts: 7128
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: How to HTTP POST from an action?
For sure it will, someday. Thanks for sharing your solution, you never know when someone else will need it.
-
- Posts: 209
- Joined: July 12th, 2020, 7:47 am
- Location: Ada, Garden City
Re: How to HTTP POST from an action?
Gooood! Hello! Also it can be reached with PowerShell:alistair@fred wrote: ↑May 8th, 2020, 8:08 am I've managed to do this using curl, as suggested.
The measure that does the work looks like this:The toggleLights variable gets set to true or false elsewhere. Note the escaping needed on the quotes to keep curl happy...Code: Select all
[measureToggleLights] Measure=Plugin Plugin=RunCommand Parameter=curl --header "Content-Type: application/json" --request POST --data "{\"lightsOn\":#toggleLights#,\"timestamp\":0,\"id\":0}" http://192.168.2.211:34568/api/v1/lights State=Show DynamicVariables=1
I hope this may be useful to someone else.
Alistair.
Code: Select all
$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams