0

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?

  • Does postman also pass the content-type header? – Chetan Ranpariya Mar 28 '20 at 06:22
  • Yes, Postman passes Content-Type 'application\json'. I spent a bit of time looking at the content type declaration thinking that the app was simply failing to identify the incoming data type. Even tried switching the existing line with a more direct "client.DefaultRequestHeaders.Add("Content-Type", "application/json");", and it just resulted in a "System.InvalidOperationException: Misused header name" error. Removing it entirely leads to the same 415, Unsupported Media Type error. – user2124846 Mar 28 '20 at 08:07
  • Could you share the action you want to send request? – Rena Mar 30 '20 at 05:39

0 Answers0