I’m currently trying to implement a method in a C# client application that allows a user to update their channel status & game at any time. Here is what I’m doing:
Application authenticates with Twitch using the ‘channel_editor’ scope (along with a couple other scopes)
Application creates a PUT WebRequest object with the OAuth token in the request header and a serialized JSON channel object in the request body
WebRequest Accept header set to “application/vnd.twitchtv.v2+json”
WebRequest Content-type header set to “application/json”
When I send this request, I get a 400 Bad Request response, which I think indicates that something in my request wasn’t set properly or the body of the request couldn’t be parsed correctly. Is there something I’m missing or doing incorrectly?
The Content-Length is automatically set when I set the request stream for the WebRequest object. However! That led me to the problem: the stream I was writing out wasn’t using the correct encoding. Once I changed it to use UTF8Encoding, it worked perfectly!
For those who are curious as to what the correct way is to set the request stream, here you go:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string postData = "{\"channel\":{\"status\":"test channel status\",\"game\":\"Minecraft\"}}";
// Set headers, method here
UTF8Encoding encoding = new UTF8Encoding();
byte[] utfBytes = encoding.GetBytes(postData);
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(utfBytes, 0, utfBytes.Length);
}
// Send the request!