Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HttpClient 415 Unsupported Media Type error

Getting 415 Unsupported Media Type Error when i try to get token from the identity server using HttpClient. But it works when I use RestClient. Why its not working with HttpClient.

Below is the code for using HttpClient

HttpClient _client = new HttpClient();
string clientId = "clientid";
string issuer = "https://ourrdomain/token";
string secretKey = "secret";

var byteArray = System.Text.Encoding.ASCII.GetBytes(clientId + ":" + secretKey);
var httpRequestMessage = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri(issuer),
    Headers = {
        { HttpRequestHeader.Authorization.ToString(), "Basic " + Convert.ToBase64String(byteArray) },
        { HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded" },
        { HttpRequestHeader.CacheControl.ToString(), "no-cache" },
        { "grant_type", "client_credentials" }
    }
};
var response = _client.SendAsync(httpRequestMessage).Result;

Below is the code using RestClient

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var request = new RestRequest();
var client = new RestClient(issuer);
string clientId = "clientid";
string issuer = "https://ourrdomain/token";
string secretKey = "secret";
var byteArray = System.Text.Encoding.ASCII.GetBytes(clientId + ":" + secretKey);
request.Method = Method.Post;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(byteArray));
request.AddParameter("grant_type", "client_credentials");
var response2 = client.Execute(request);

>Solution :

application/x-www-form-urlencoded

The 415 Unsupported Media Type error in your HttpClient code is likely due to the way the content type and body are being set. In the HttpClient code, you’re setting the ContentType header directly, but not providing the body content in the correct format

application/x-www-form-urlencoded.

The HttpRequestMessage should include an HttpContent object with the content type set correctly and the body encoded as form data.

Here’s how you can fix the HttpClient code:

HttpClient _client = new HttpClient();
string clientId = "clientid";
string issuer = "https://ourrdomain/token";
string secretKey = "secret";

var byteArray = System.Text.Encoding.ASCII.GetBytes($"{clientId}:{secretKey}");
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
_client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };

var requestData = new Dictionary<string, string>
{
    { "grant_type", "client_credentials" }
};
HttpContent content = new FormUrlEncodedContent(requestData);

var response = _client.PostAsync(issuer, content).Result;
  • The credentials are added to the default request headers, which is a
    cleaner way of adding headers that don’t change often.
  • The form data is being sent as HttpContent using
    FormUrlEncodedContent, which correctly sets the Content-Type header
    to application/x-www-form-urlencoded.
  • We use PostAsync method of HttpClient which takes care of setting the
    proper Content-Type header and encoding the content.

Make sure to handle the Result properly, preferably by using async/await instead of .Result to avoid deadlocks and improve performance. Here’s an example using async/await:

public async Task<TokenResponse> GetTokenAsync()
{
    // ... other code ...

    var response = await _client.PostAsync(issuer, content);
    if (response.IsSuccessStatusCode)
    {
        var json = await response.Content.ReadAsStringAsync();
        // Deserialize the JSON response to a token response object
        var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(json);
        return tokenResponse;
    }
    else
    {
        // Handle the error, log it, etc.
        return null;
    }
}

In this async method, we’re handling the HTTP call asynchronously and returning the deserialized token response object. Always check if the response is successful before attempting to read the content.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading