HTTP 415 unsupported media type error when calling Web API 2 endpoint

less than 1 minute read

You have to send the header to accept the content type.

HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("Bearer", Token);
var httpRequestMessage = new HttpRequestMessage
{
    Content = new StringContent("[]", Encoding.UTF8, "application/json")
};
httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.PostAsync(URL, httpRequestMessage.Content).Result;

In Ajax,

$http({
            method: 'POST',
            url: '/api/Salesman',
            headers: { 'Accept': 'application/json' }

Leave a comment