Dotnet core – 415 unsupported media type error? Easy fix2 min read

Prologoue

Hello friends! It’s been a while, again! I hope you are all going well and that this new 2021 is going to be better than the last 2020 year, for all of us.

My New Years’ resolutions are to head back to posting each week blog post and to make one Youtube video per month!
So let’s see how’s that going to work out. πŸ™‚

So here is my new blog post in 2021:

415 Unsupported Media Type error

If you just started with .NET API, most certainly you will experience this kind of an error.
You will have your method ready with some param and [FromBody] attribute set and you will expect JSON results from your method.

I.e.:

[ApiController]
public class MyController : Controller {
[HttpPost]
public async Task<IActionResult> SomeMethod([FromBody] CustomModel model) { //... }
}

and when you fire a post from Postman, baaaam.


(Image source: https://www.keycdn.com/support/415-unsupported-media-type)

You get this: 415 unsupported media type error and you react like: β€œWTF!?!? What did I do wrong!?!”. Scary error… I mean, all errors are scary, right!? πŸ™‚ But don’t worry, I will show you the fix! But firstly let me explain, why did you get this error!

Here’s the thing, by default when you fire a post in Postman, request type is set to β€œTEXT”.

And on the other side our (Asp.Net Core) backend cannot distinguish between json or text input params.

So in order to fix this issue we have 2 (two) options:

Fix number one:

We must ensure that we’re using raw and that our request is in β€œJSON” format. 

But there is another one as well. We can use another fix by creating middleware in our Asp.Net Core application.

Fix number two:

So we will create fix to accept body params inputs without headers: 

Accept/Content-Types: application/json

And to use this fix, we need to put the following code inside our ConfigureServices method in Startup.cs:

            services.AddMvc(options =>
{
options.AllowEmptyInputInBodyModelBinding = true;
foreach (var formatter in options.InputFormatters)
{
if (formatter.GetType() == typeof(SystemTextJsonInputFormatter))
((SystemTextJsonInputFormatter)formatter).SupportedMediaTypes.Add(
Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain"));
}
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
What did we just do?

Well, we created our custom middleware where we provided our custom formatter. 

Basically we added Json formatting to plain text.

This way we removed the necessity for adding JSON header when sending our body input params.

(Visited 10,689 times, 1 visits today)

3 Comments

  1. Andriele 13 September 2022 at 13:59

    Hi, thank u so much, it helped me a lottt.
    I’m gladly that someone on the internet actually knows how to explain some instruction to someone that doesn’t know what to do πŸ™‚

    1. Denis Jakus 13 September 2022 at 17:37

      Thank you so much for your comment! I am here to help! πŸ™‚

  2. cesar 12 March 2023 at 17:51

    gracias, justo lo que andaba buscando.

Leave a comment

Your email address will not be published. Required fields are marked *