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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jayraj ChhayaPosted Dec 26, 2023, 10:25 AM
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: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.
Subarta RayPosted Dec 26, 2023, 5:58 AM
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
}
Satyaprakash SamantarayPosted Dec 21, 2023, 8:20 AM
Hi,
Basically the media type u passed that is invalid. Please check the valid media type formatters
Amit MohantyPosted Dec 21, 2023, 7:36 AM
Please provide enough code so others can better understand the problem.