Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
myReq = HttpWebRequest.Create(Twitch_BaseURL & "channels/" & Twitch_UserID)
myReq.Method = "PUT"
myReq.ContentType = "application/json"
myReq.Headers.Add("Client-ID:" & Twitch_ClientID)
myReq.Accept = "application/vnd.twitchtv.v5+json"
Dim myData As String = "channel[status]=Please+work!&channel[game]=Something"
myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
On the line myResp = myReq.GetResponse I get an #500 Internal Server Error
I tried different methods but doesn’t get it to work,
Client-ID, Twitch_BaseURL and Twitch_UserID are fine, because they work on request methods.
What @Dist pointed out is correct: you’re missing the Authorization header.
But also your Content-Type header should be application/x-www-form-urlencoded as that is the format you’re using in your request body. Alternatively make your body JSON and use application/json like in your original example.
To help others, this should then be the final procedure.
Public Sub SetChannelInfo(Game As String, Status As String)
Try
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
myReq = HttpWebRequest.Create(Twitch_BaseURL & "channels/" & Twitch_UserID)
myReq.Method = "PUT"
myReq.ContentType = "text/plain"
myReq.Headers.Add("Client-ID:" & Twitch_ClientID)
myReq.Headers.Add("Authorization: OAuth " & My.Settings.Twitch_AuthenticationCode)
myReq.Accept = "application/vnd.twitchtv.v5+json"
Dim myData As String = "channel[status]=" & Status & "&channel[game]=" & Game
Dim Buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(myData)
myReq.GetRequestStream.Write(Buffer, 0, Buffer.Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
Catch ex As Exception
MsgBox("Some error on setting Channel Info")
End Try
End Sub