I am using HttpClient to consume an external API from an ASP.NET Web API controller. I am not using authentication, just a token, so I have:
using (var httpClient = new HttpClient()) {
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.GetAsync(endpoint);
}
I am getting the response always in XML format but I am sending header with "application/json".
Am I missing something it this is a problem with the external API?
What else can I try to get the response in JSON?
It's up to the API developer(s) to respect the media type (application/json). It is possible for a developer to explicitly return XML when a client requests JSON (if they feel like trolling), though in this case it is probably just giving you the default format because they don't check the header value.
Check the docs or contact them directly to confirm they return data in JSON format and how to format the request to get JSON.