Prasad Shigwan
Prasad Shigwan

Reputation: 552

POSTMAN - post request giving "The request entity's media type 'application/json' is not supported for this resource."

Web API code:

[HttpPost]
[ODataRoute("GetCMMAutoForLoggedInUser")]
public IHttpActionResult GetCMMAutoForLoggedInUser(CMMPermissionRequest request)
{
    var data = this.CommonDomain.GetCMMAutoForLoggedInUser(request);

    return Ok(data);
}

in body I'm specifying below JSON:

{ "EnterPriseId": "prasad.kiran.shigwan",
"LocationLevelId": "5",
"LocationLevelValue": "SZ"
} 

but getting below exception in POSTMAN tool:

{
    "error": {
        "code": "",
        "message": "The request entity's media type 'application/json' is not supported for this resource.",
        "innererror": {
            "message": "No MediaTypeFormatter is available to read an object of type 'CMMPermissionRequest' from content with media type 'application/json'.",
            "type": "System.Net.Http.UnsupportedMediaTypeException",
            "stacktrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync\[T\](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
        }
    }
}

Refer below screenshot for json from POSTMAN:

enter image description here

Upvotes: 0

Views: 9468

Answers (1)

CodeFuller
CodeFuller

Reputation: 31312

ASP.Net Web API has several built-in media type formatters, which are responsible for serializing/deserializing body content from json, xml, etc.

JSON formatter is always on by default, both for ASP.Net Web API 2 and ASP.Net Core Web API. The POST from the question works fine for me for default ASP.Net Web API project, however I can reproduce your problem if remove JSON formatter in WebApiConfig.Register:

config.Formatters.Remove(config.Formatters.JsonFormatter);

Seems like you have similar code in Web API configuration that unregisters JsonFormatter. If this is not the case, set breakpoint in WebApiConfig.Register and check what are the values inside config.Formatters collection. Here is default list with JsonFormatter:

enter image description here

If for some reason JsonFormatter will be missing in your case, you could register it explicitly:

config.Formatters.Add(new JsonMediaTypeFormatter());

Upvotes: 1

Related Questions