Ok, I've been beating my head against this one for hours, read the docs, and googled the hell out of it. I'm stumped. I'm fairly new to ASP.Net Core/C# and am trying to make a fairly straightforward HTTP Get call to a remote service from an object in my web app. The service requires that I set an "APIKey" header to authenticate against their servers, but is otherwise fairly vanilla and is returning a generic JSON collection. Rather than receiving the JSON, I'm getting a failed status with the reason phrase "Unsupported Media Type" and the status code "415". I can query the service with no problems in Postman.
The RequestMessage in the response is :
{Method: GET, RequestUri: 'url_removed_for_stack', Version: 2.0, Content: <null>, Headers:
{
Accept: application/json
Accept-Encoding: gzip
Accept-Encoding: deflate
Accept-Encoding: gzip
Accept-Encoding: deflate
APIKey: apikey_removed_for_stack
}}
The calling method, in its entirety:
private async Task<JsonValue> GetAsync(string uri, string apiKey, int timeout)
{
var handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (var client = new HttpClient(handler))
{
//remove the default header
client.DefaultRequestHeaders.Clear();
client.Timeout = TimeSpan.FromMilliseconds(timeout);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.Add("APIKey", apiKey);
try
{
var response = await client.GetAsync(uri);
if (!response.IsSuccessStatusCode)
{
isError = true;
errorMessage = "GetAsync Response Error: " + response.ReasonPhrase;
return await Task.FromResult(0);
}
var content = await response.Content.ReadAsStringAsync();
return await Task.Run(() => JsonValue.Parse(content));
}
catch (Exception ex)
{
isError = true;
errorMessage = ex.ToString();
return await Task.FromResult(0);
}
}
}
What am I missing?