How to add header content-type profile parameter

I am trying to add custom header content-type to HttpClient Post request profile parameter but I am getting an exception

System.ApplicationException: Status code: UnsupportedMediaType, Error: Unsupported Media Type

Below is the code in c#

             var cert = new X509Certificate2(Path.Combine("certificate.pfx"), "password");             var handler = new HttpClientHandler();             handler.ClientCertificates.Add(cert);             var client = new HttpClient(handler);             string requestcontent = "{ \"branch_reference\": \"test_branch_ref\" }";              using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, ""))             {                 request.Content = new StringContent(requestcontent, Encoding.UTF8, "application/json");                 request.Content.Headers.ContentType.Parameters.Add(                     new NameValueHeaderValue("profile", "\"\"")                     );                 var response = await client.SendAsync(request);                 if (response.IsSuccessStatusCode)                 {                     var responseContent = await response.Content.ReadAsStringAsync();                     var data = JsonDocument.Parse(responseContent);                     return data;                 }                 throw new ApplicationException($"Status code: {response.StatusCode}, Error: {response.ReasonPhrase}");             }  

Edit: It is now working. Updated the working code above for anyone to use

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.