[Solved]VBA - Set State with Http API

Hi All,

I am new and have only a little bit of VBA programming experience. I am trying to control my Lifx light with Excel VBA. Now, I can use below code to set Power or Color or Brightness. However, I cannot combine multiple setting at the same request which means I cannot use Color+Duration because I have no idea the format on the Data part.


Sub Lighttest()
Const sUrl As String = “https://api.lifx.com/v1/lights/xxxxxxx/state” < ----Selector

Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String

On Error GoTo Err_Lighttest

Set oRequest = New WinHttp.WinHttpRequest
With oRequest
.Open “PUT”, sUrl, True
.SetRequestHeader “Content-Type”, “application/x-www-form-urlencoded; charset=UTF-8”
.SetRequestHeader “Authorization”, “Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” '<----Token
.Send “power=on” '<---------Action
.WaitForResponse
sResult = .ResponseText
Debug.Print sResult
sResult = oRequest.Status
Debug.Print sResult
End With

Exit_Lighttest:
On Error Resume Next
Set oRequest = Nothing
Exit Sub

Err_Lighttest:
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_Lighttest

End Sub


I tried belows…

.Send “power=on color=red” or .Send “power=on, color=red” or .Send “power=on , color=red”

Response:
{“error”:“power does not have a valid value”,“errors”:[{“field”:“power”,“message”:[“does not have a valid value”]}]}
422

I can’t figure out what to use to separate multiple the actions. Please help on this. Thank you.

Oh…I figure it out by looking at the PHP example…Use &…

.send “power=on&color=blue&duration=5”

The next thing is coverting into Google Script…