Announcement

Collapse
No announcement yet.

hs.URLAction PUT request not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    hs.URLAction PUT request not working

    Hi everyone I seem to be having an issue with the put request type.

    HTML Code:
    Sub Main(Parm As Object)
        
        dim result as String
        dim website as String = "https://api.lifx.com/v1/lights/group:XXXXX's Room/state"
        dim request as String = "PUT"
        dim information as String = '{"power" : "on", "brightness" : "1"}'
        dim header as String = "Authorization:Bearer XXXXX(FILLED IN)XXXXX"
        
        result = hs.URLAction(website, request, information, header)
    End Sub
    I get this compliation error

    Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: 'information' is a type and cannot be used as an expression.
    Dec-17 10:13:41 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: Expression expected.
    Dec-17 10:13:41 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.




    EDIT: I can now confirm with testing from http://requestb.in that HomeSeer does not make any put requests.
    Last edited by matt246; December 17, 2015, 05:53 PM.

    #2
    Add this just above "End Sub"
    hs.Writelog("PUT output", result)
    ... so that we can see what's going on.


    And thanks for the tip! I didn't know LIFX finally released their api. I'll take a look at it, and see if I can start on a plugin.
    HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
    Running on Windows 10 (64) virtualized
    on ESXi (Fujitsu Primergy TX150 S8).
    WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

    Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

    Comment


      #3
      The problem is this line:

      PHP Code:
      dim information as String '{"power" : "on", "brightness" : "1"}' 
      Change it to:

      PHP Code:
       Dim information As String "{""power"" : ""on"", ""brightness"" : ""1""}" 
      Jon

      Comment


        #4
        Originally posted by jon00 View Post
        The problem is this line:

        PHP Code:
        dim information as String '{"power" : "on", "brightness" : "1"}' 
        Change it to:

        PHP Code:
         Dim information As String "{""power"" : ""on"", ""brightness"" : ""1""}" 
        Tried this didnt work.



        Also the PUT output nothing when I got it to log to HS :S

        Comment


          #5
          Did the compiling error go away?
          Jon

          Comment


            #6
            Originally posted by jon00 View Post
            Did the compiling error go away?
            Yeah it did with this code

            HTML Code:
            Sub Main(Parm As Object)
                
                Dim result As String
                Dim website As String = "https://api.lifx.com/v1/lights/group:XXXXX's Room/state"
                Dim request As String = "PUT"
                Dim information As String = "{""power"" : ""on"", ""brightness"" : ""1""}"
                Dim header As String = "Authorization: Bearer XXXXXXXX"
                
                result = hs.URLAction(website, request, information, header)
                
                hs.WriteLog("Info", result + "end")
                
                
            End Sub

            Only thing is, is that the PUT still doesnt do anything?

            Comment


              #7
              Originally posted by matt246 View Post
              Hi everyone I seem to be having an issue with the put request type.

              HTML Code:
              Sub Main(Parm As Object)
                  
                  dim result as String
                  dim website as String = "https://api.lifx.com/v1/lights/group:XXXXX's Room/state"
                  dim request as String = "PUT"
                  dim information as String = '{"power" : "on", "brightness" : "1"}'
                  dim header as String = "Authorization:Bearer XXXXX(FILLED IN)XXXXX"
                  
                  result = hs.URLAction(website, request, information, header)
              End Sub
              I get this compliation error

              Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: 'information' is a type and cannot be used as an expression.
              Dec-17 10:13:41 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: Expression expected.
              Dec-17 10:13:41 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\Lifx.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.




              EDIT: I can now confirm with testing from http://requestb.in that HomeSeer does not make any put requests.


              An update I got a message from support saying this code should work, but im not exactly sure what and where im supposed to edit to make it work?


              Code:
              Dim httpWebRequest = DirectCast(WebRequest.Create("https://apiURL"), HttpWebRequest)
              httpWebRequest.ContentType = "text/json"
              httpWebRequest.Method = "PUT"
              
              Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
              ' Need to put data here to pass to the API.**
              Dim json As String = streamWriter.Write(json).Target.Target
              End Using
              Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
              Using streamReader = New StreamReader(httpResponse.GetResponseStream())
              Dim responseText = streamReader.ReadToEnd()
              'Now you have your response.
              'or false depending on information in the response
              Return True
              End Using

              If anyone could help me out that would be great!
              Thanks

              Comment


                #8
                Try something like:


                PHP Code:
                    Imports System.IO
                    Imports System
                .Net

                    Sub Main
                (ByVal Parm As Object)
                        
                Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)
                        
                httpWebRequest.ContentType "application/json"
                        
                httpWebRequest.Method "PUT"

                        
                Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                            
                Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                            
                streamWriter.Write(json)
                        
                End Using

                        Dim httpResponse 
                DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                        
                Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                            
                Dim responseText StreamReader.ReadToEnd()
                            
                hs.WriteLog("Response"responseText)
                        
                End Using
                    End Sub 
                Jon

                Comment


                  #9
                  ... we can use "Imports" in scripting now?
                  Or is it just for references already added?
                  HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
                  Running on Windows 10 (64) virtualized
                  on ESXi (Fujitsu Primergy TX150 S8).
                  WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

                  Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

                  Comment


                    #10
                    Originally posted by jon00 View Post
                    Try something like:


                    PHP Code:
                        Imports System.IO
                        Imports System
                    .Net

                        Sub Main
                    (ByVal Parm As Object)
                            
                    Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)
                            
                    httpWebRequest.ContentType "application/json"
                            
                    httpWebRequest.Method "PUT"

                            
                    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                                
                    Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                                
                    streamWriter.Write(json)
                            
                    End Using

                            Dim httpResponse 
                    DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                            
                    Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                                
                    Dim responseText StreamReader.ReadToEnd()
                                
                    hs.WriteLog("Response"responseText)
                            
                    End Using
                        End Sub 
                    Thanks, the only thing is is that I still need to send the authorisation in the header. I ended up making a script in js as I wanted to keep working on it but if possible could you provide the code with authorisation headers too as im sure it would help out those coding in .net

                    Comment


                      #11
                      Originally posted by Moskus View Post
                      ... we can use "Imports" in scripting now?
                      Or is it just for references already added?
                      You have always been able to add Imports to scripts. They need to be used for certain references added by default and those added via the scriptingreferences entry in settings.ini
                      Jon

                      Comment


                        #12
                        Originally posted by matt246 View Post
                        Thanks, the only thing is is that I still need to send the authorisation in the header. I ended up making a script in js as I wanted to keep working on it but if possible could you provide the code with authorisation headers too as im sure it would help out those coding in .net
                        Try this then:

                        PHP Code:
                            Sub Main(ByVal Parm As Object)
                                
                        Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)

                                
                        httpWebRequest.ContentType "application/json"
                                
                        httpWebRequest.Method "PUT"
                                
                        httpWebRequest.Headers.Add("Authorization""Bearer XXXXX(FILLED IN)XXXXX")

                                
                        Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                                    
                        Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                                    
                        streamWriter.Write(json)
                                
                        End Using

                                Dim httpResponse 
                        DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                                
                        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                                    
                        Dim responseText StreamReader.ReadToEnd()
                                    
                        hs.WriteLog("Response"responseText)
                                
                        End Using
                            End Sub 
                        Jon

                        Comment


                          #13
                          Originally posted by jon00 View Post
                          Try this then:

                          PHP Code:
                              Sub Main(ByVal Parm As Object)
                                  
                          Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)

                                  
                          httpWebRequest.ContentType "application/json"
                                  
                          httpWebRequest.Method "PUT"
                                  
                          httpWebRequest.Headers.Add("Authorization""Bearer XXXXX(FILLED IN)XXXXX")

                                  
                          Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                                      
                          Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                                      
                          streamWriter.Write(json)
                                  
                          End Using

                                  Dim httpResponse 
                          DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                                  
                          Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                                      
                          Dim responseText StreamReader.ReadToEnd()
                                      
                          hs.WriteLog("Response"responseText)
                                  
                          End Using
                              End Sub 
                          I'm using the script you offered here, with my own info plugged in, of course, and getting errors:

                          Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: 'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'. 'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.


                          Any suggestions to help out a newbie?

                          Comment


                            #14
                            You need to add the Imports Statements at the top of the script i.e.

                            PHP Code:
                                Imports System.IO
                                Imports System
                            .Net

                               Sub Main
                            (ByVal Parm As Object)
                                    
                            Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)

                                    
                            httpWebRequest.ContentType "application/json"
                                    
                            httpWebRequest.Method "PUT"
                                    
                            httpWebRequest.Headers.Add("Authorization""Bearer XXXXX(FILLED IN)XXXXX")

                                    
                            Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                                        
                            Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                                        
                            streamWriter.Write(json)
                                    
                            End Using

                                    Dim httpResponse 
                            DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                                    
                            Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                                        
                            Dim responseText StreamReader.ReadToEnd()
                                        
                            hs.WriteLog("Response"responseText)
                                    
                            End Using
                                End Sub 
                            Jon

                            Comment


                              #15
                              Originally posted by jon00 View Post
                              You need to add the Imports Statements at the top of the script i.e.

                              PHP Code:
                                  Imports System.IO
                                  Imports System
                              .Net

                                 Sub Main
                              (ByVal Parm As Object)
                                      
                              Dim httpWebRequest DirectCast(WebRequest.Create("https://api.lifx.com/v1/lights/group:XXXXX's Room/state"), HttpWebRequest)

                                      
                              httpWebRequest.ContentType "application/json"
                                      
                              httpWebRequest.Method "PUT"
                                      
                              httpWebRequest.Headers.Add("Authorization""Bearer XXXXX(FILLED IN)XXXXX")

                                      
                              Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                                          
                              Dim json As String "{""power"" : ""on"", ""brightness"" : ""1""}"
                                          
                              streamWriter.Write(json)
                                      
                              End Using

                                      Dim httpResponse 
                              DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
                                      
                              Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                                          
                              Dim responseText StreamReader.ReadToEnd()
                                          
                              hs.WriteLog("Response"responseText)
                                      
                              End Using
                                  End Sub 
                              Hi Jon, I'm trying this script as well and get the same errors:

                              Code:
                              Mar-07 11:21:49 PM	 	Error	Compiling script C:\Program Files\HomeSeer HS3\scripts\wdtvlivetest.vb: Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: 'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'. 'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.
                              Mar-07 11:21:49 PM	 	Error	Compiling script C:\Program Files\HomeSeer HS3\scripts\wdtvlivetest.vb: Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: 'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'. 'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.
                              Mar-07 11:21:49 PM	 	Error	Compiling script C:\Program Files\HomeSeer HS3\scripts\wdtvlivetest.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
                              I have

                              Code:
                              Imports System.IO
                              Imports System.Net
                              at the top of my script. Any ideas?

                              Thanks
                              Al
                              HS 4.2.8.0: 2134 Devices 1252 Events
                              Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                              Comment

                              Working...
                              X