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:
{"type":"channel.subscribe","version":"1","condition":{"broadcaster_user_id":userID},"transport":{"method":"websockets","session_id":sessionID}}