It is currently January 16th, 2021, 7:56 am

How to HTTP POST from an action?

Help with creating, editing & fixing problems with skins
alistair@fred
Posts: 3
Joined: May 5th, 2020, 8:20 am

How to HTTP POST from an action?

Post by alistair@fred »

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.
User avatar
balala
Rainmeter Sage
Posts: 12062
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to HTTP POST from an action?

Post by balala »

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.
Can you do this from your browser, via command line or anyway else?
User avatar
SilverAzide
Rainmeter Sage
Posts: 1086
Joined: March 23rd, 2015, 5:26 pm

Re: How to HTTP POST from an action?

Post by SilverAzide »

alistair@fred wrote: May 5th, 2020, 8:36 am Any suggestions or hints would be appreciated.
WebParser is GET-only. As mentioned above, you can use curl from a PowerShell command line (using a CommandMeasure).
Gadgets Wiki GitHub More Gadgets...
alistair@fred
Posts: 3
Joined: May 5th, 2020, 8:20 am

Re: How to HTTP POST from an action?

Post by alistair@fred »

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...
alistair@fred
Posts: 3
Joined: May 5th, 2020, 8:20 am

Re: How to HTTP POST from an action?

Post by alistair@fred »

I've managed to do this using curl, as suggested.

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

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.
User avatar
Yincognito
Posts: 2948
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How to HTTP POST from an action?

Post by Yincognito »

alistair@fred wrote: May 8th, 2020, 8:08 amI hope this may be useful to someone else.

Alistair.
For sure it will, someday. Thanks for sharing your solution, you never know when someone else will need it. :thumbup:
dvo
Posts: 1029
Joined: February 7th, 2016, 6:08 am

Re: How to HTTP POST from an action?

Post by dvo »

tnx alistair@fred,

for sharing this :) now i can go on with my script :) got it altert by ashthespy on volumio to use in some some skin i'm writing LOL :rolmfao:
and balala i think i need some more help along the way ...
User avatar
deflore08
Posts: 99
Joined: July 12th, 2020, 7:47 am

Re: How to HTTP POST from an action?

Post by deflore08 »

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:

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

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.
Gooood! Hello! Also it can be reached with PowerShell:

Code: Select all

$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams
I will damn need this a bit later, so thank you. :)