HTTP Status 415 - Unsupported Media Type

Asked

Viewed 3,021 times

1

Follows code:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
var result = await httpClient.DeleteAsync(new Uri("URL do Site ..."));

See the return:

HTTP Status 415 - Unsupported Media Type Type Status Report

Message Cannot consume content type

Description The origin server is refusing to service the request because the payload is in a format not supported by this method on the target Resource.

Apache Tomcat/8.5.23

Placed "Accept", "application/json" and yet it didn’t work. I seem to be missing put application/json in the DeleteAsync().

Some solution ?

2 answers

1

Buddy, good afternoon

You must specify the HTTP method you refer to, such as Http Delete, and if necessary, inform that the information is via Body, or query string and so on:

HttpRequestMessage request = new HttpRequestMessage
{
    Content = new StringContent("[YOUR JSON GOES HERE]", Encoding.UTF8, "application/json"),
    Method = HttpMethod.Delete,
    RequestUri = new Uri("[YOUR URL GOES HERE]")
};

await httpClient.SendAsync(request);

I hope I’ve helped.

0


I found another solution:

HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Delete, "URL do Site ...")
{
    Content = new StringContent(string.Empty, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await httpClient.SendAsync(httpRequest);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.