For a project, i've to use an API that uses the GET verb and which necessarily requires the "Content-type" property, but this isn't standard and i'd like to set this property to "application/json".
I'm using the C# HttpClient and after looking in the whole universe, I can't find a way to do it. I always have a "ProtocolViolationException", obviously...
Is there a way to use a "Content-type" and a "GET" request with HttpClient ?
I'm using this code
public async Task<HttpResponseMessage> GetAsync(string uri, double timeout = 0, string token = null)
{
using(var handler = new HttpClientHandler())
{
if(handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
using(var client = new HttpClient(handler))
{
var request = new HttpRequestMessage(HttpMethod.Get, uri);
HttpResponseMessage httpResponseMessage = null;
if(timeout > 0)
{
client.Timeout = TimeSpan.FromSeconds(timeout);
}
if(!string.IsNullOrWhiteSpace(token))
{
request.Headers.Add("authorization", token);
}
request.Content = new StringContent("");
request.Content.Headers.Remove("Content-type");
request.Content.Headers.Add("Content-type", "application/json");
httpResponseMessage = await client.SendAsync(request);
return httpResponseMessage;
}
}
}
Thanks in advance :)