Hi Friends,
I am passing custom header application/api.spidex.v1+json in Web Api Get Method call.
Getting unsupportd media type error.
I used new mediatypeheaderQuerywithvalue but not working.
Please tell me how to resolve this.
Hi Friends,
I am passing custom header application/api.spidex.v1+json in Web Api Get Method call.
Getting unsupportd media type error.
I used new mediatypeheaderQuerywithvalue but not working.
Please tell me how to resolve this.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Dec 21, 2023, 2:36 AM EST
Please provide enough code so others can better understand the problem.
Satyaprakash SamantarayRank #101 · 21.1K reputationPosted Dec 21, 2023, 3:20 AM EST
Hi,
Basically the media type u passed that is invalid. Please check the valid media type formatters
Subarta RayRank #336 · 7.4K reputationPosted Dec 26, 2023, 12:58 AM EST
Hi Prasana ,
To resolve the "unsupported media type" error in Web API, ensure your client includes the correct "Accept" header, such as "application/api.spidex.v1+json." On the server side, use
MediaTypeWithQualityHeaderValuefor media type negotiation. Example:[HttpGet]
public IHttpActionResult YourMethod()
{
var mediaType = new MediaTypeWithQualityHeaderValue("application/api.spidex.v1+json");
if (!Request.Headers.Accept.Contains(mediaType))
return StatusCode(HttpStatusCode.UnsupportedMediaType);
// Your logic here
}
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 26, 2023, 5:25 AM EST
Hi prasanna p,
To resolve the "Unsupported Media Type" error when using a custom header in an HttpClient request, you need to ensure that the server can understand and process the media type specified in the header. Here are a few steps you can follow to troubleshoot and resolve this issue:
Verify the Media Type: Double-check that the media type "application/api.spidex.v1+json" is correct and supported by the server. Ensure that the server is configured to handle this media type.
Set the Content-Type Header: In addition to the custom header, you also need to set the "Content-Type" header to specify the media type of the request body. You can use the
DefaultRequestHeadersproperty of the HttpClient instance to set the headers. Here's an example:using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("application/api.spidex.v1+json", "your-header-value"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Make the GET request HttpResponseMessage response = await client.GetAsync("your-api-url"); // Process the response // ... }Check Server Configuration: Ensure that the server-side code is correctly configured to handle the custom media type. If you have control over the server code, make sure it can parse and process the custom media type.
Test with a Different Tool: To isolate the issue, try using a different tool or client (e.g., Postman) to make the same request with the custom header. If the request works with the other tool, it indicates that the issue might be specific to your code or HttpClient configuration.