Announcement

Collapse
No announcement yet.

Can't seem to get an HTTP PUT working for some reason...

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

    Can't seem to get an HTTP PUT working for some reason...

    Hi there,

    I can't seem to figure out the errors I'm getting when trying to do an HTTP PUT to a website. I've pretty much followed the examples I've seen online. Do I not have something installed on my HS3 Zee 2 that I need to run this? I suppose I can just call a curl command but I would prefer to use native code to do this. Any thoughts?

    Thanks, Chris

    Code:
    IMPORTS System.IO
    IMPORTS System.Net
    
    Sub Main(Parm As Object)
    
    Dim httpWebRequest = DirectCast(WebRequest.Create("http://192.168.1.50:3000/state/circuit/toggleState"), HttpWebRequest)
    
    httpWebRequest.ContentType = "application/json"
    
    httpWebRequest.Method = "PUT"
    
    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream().ToString)
    
            Dim json As String = "{""id"" : ""3""}"
    
            streamWriter.Write(json)
    
    End Using
    
    Dim httpResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
    Using streamReader = New StreamReader(httpResponse.GetResponseStream().ToString)
    Dim responseText = StreanReader.ReadToEnd()
    hs.WriteLog("Response", responseText)
    
    End Using
    
    End Sub
    When I manually run this, I get the following logs:
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: 'End Namespace' must be preceded by a matching 'Namespace'.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Identifier expected.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Expected 'End'.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: Expected 'End'.
    Sep-07 5:35:52 PM Error Compiling script /usr/local/HomeSeer/scripts/back_ll_on.vb: End of statement expected.

    #2
    Whatever I did wrong, I guess I'll never know. I did this script instead and it seems to work although it didn't yesterday. Maybe I was tired...

    Code:
    Imports System.IO
    Imports System.Net
    Imports System.Web
    
    Sub Main(ByVal Parms As Object)
    
    Dim logName As String = "Pentair"
    Dim debug As Boolean = True
    Dim json As String = "{""id"" : ""3""}"
    
    If Debug Then hs.writelog(logName, "Command: " & json)
    
    Dim strURL as String = "http://192.168.1.50:3000/state/circuit/toggleState"
    Dim myWebReq As HttpWebRequest
    Dim myWebResp As HttpWebResponse
    Dim encoding As New System.Text.UTF8Encoding
    Dim sr As StreamReader
    
    Try
    Dim data As Byte() = encoding.GetBytes(json)
    myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
    myWebReq.ContentType = "application/json; charset=utf-8"
    myWebReq.ContentLength = data.Length
    myWebReq.Method = "PUT"
    Dim myStream As Stream = myWebReq.GetRequestStream()
    
    If data.Length > 0 Then
    myStream.Write(data, 0, data.Length)
    myStream.Close()
    End If
    
    myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
    sr = New StreamReader(myWebResp.GetResponseStream())
    Dim responseText As String = sr.ReadToEnd()
    
    If Debug Then hs.WriteLog(logName, "Response: " & responseText)
    
    Catch ex As Exception : hs.writelog(logName, "Error: " & ex.Message.ToString)
    
    End Try
    
    End Sub

    Comment


      #3
      Dim responseText = StreanReader.ReadToEnd()

      Comment


        #4
        Originally posted by rge View Post
        Dim responseText = StreanReader.ReadToEnd()
        Oh my. Thank you much. I looked at the code for an hour and didn't see it.

        Comment

        Working...
        X