Hi there, I’m building an app in C# Windows Form that connects to Eventsub Websockets to request new Subscriber events. The app will then increment a timer based on these events.
However, in my app, I have an error sending the HTTPS request to subscribe to the channel.subscribe event. The error says: The value specified in the method field is not valid, with a status code of 500.
I’m just not sure what this means. I’m sending an HTTP POST request so that can’t be what it means by method, and I’m sending a method value of websockets in the transport object I’m sending, along with the Websockets session ID.
What else could I do to fix this error? If it matters, I’m using the System.Net.Http.HttpClient to send the request.
I am actually setting the Content-Type manually to application/json after setting the Content. I actually double-checked the spelling of that header value but it seems to be fine. As a debugging step, I added a console log line of the resulting Content-Type and it told me the Request.Content.Headers.Content-Type is application/json (spelling copied from console).
I’ll outline everything I’m doing if someone more versed in this could help me out
//This is a custom class to build the JSON object for the request body when serialized
// topic is "channel.subscribe", userID is a hardcoded string userID (but I didn't want to share) and the sessionID comes from the welcome message
SubscriptionRequest request = new(topic, "1", new SubCondition(userID), new SubTransport(sessionID));
HttpRequestMessage httpRequest = new(HttpMethod.Post, "https://api.twitch.tv/helix/eventsub/subscriptions");
httpRequest.Headers.Authorization = AuthenticationHeaderValue.Parse($"Bearer {Settings1.Default.AccessToken}");
httpRequest.Headers.Add("Client-Id", Resources.ClientID);
httpRequest.Content = new StringContent(JsonConvert.SerializeObject(request));
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
Debug.WriteLine($"Request.Content.Headers: {httpRequest.Content.Headers}");
/*HttpResponseMessage response = httpClient.Send(httpRequest);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine($"Successfully subscribed to topic {topic}");
}
else
{
Debug.WriteLine($"Error subscribing to topic {topic}: {await response.Content.ReadAsStringAsync()}");
}*/
While writing this post, I googled the HTTP client library and found there was an overload that allowed me to set the Content-Type while in the StringContent constructor like so:
httpRequest.Content = new StringContent(JsonConvert.SerializeObject(request), null, "application/json");
The null parameter in this case is a text encoding, which would result in a Content-Type of application/json;charset=utf-8 if set, which I wasn’t sure the server would accept.
Also, here’s the string JSON that is being sent to the server, showing the variables in their place:
Checking your full payload, the method should be websocket not websockets
Missed this in the original post as I got to the HTTP code and error message and skimmed the rest (and it was pre coffee post DST getting up an hour earlier 13 hours ago)
No problem, thanks for checking this out again. I was using websockets plural because that’s what it said on Managing Subscriptions | Twitch Developers . Perhaps the documentation needs to be updated? I’ll update my code and let you know if that worked.