Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 2.2 Web API getting 415 Unsupported Media type on a GET?

I have upgraded my WebApi project to .net core 2.2 and since then, all of my controllers are pulling 415 Unsupported Media type from every single GET call. Which is super strange because 415 is generally something reserved for POST in my experience.

If I downgrade back to 2.1, problem goes away. I've posted code below of my controller setup, and the basic startup config.

    [Route("v1/[controller]")]
    [Produces("application/json")]
    [Consumes("application/json")]
    [Authorize]
    public class JobsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetJobSummaryByUserId([FromQuery] PagedJobRequest pagedJobRequest)
        {
            if (pagedJobRequest.UserId == Guid.Empty)
            {
                pagedJobRequest.UserId = _jwtUtility.GetIdentityId();
            }
            if (!_jwtUtility.DoesJwtIdentityIdMatch(pagedJobRequest.UserId) && !_jwtUtility.IsUserInRole("Administrator"))
            {
                return Unauthorized();
            }

            var returnObj = _jobsService.GetJobSummariesByUserId(pagedJobRequest);

            return Ok(returnObj);
        }
}

In Startup.cs:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddCors(x => x.AddPolicy("MVRCors", y => y.AllowCredentials().AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));
        services.AddEntityFrameworkSqlServer();
        }
   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(s =>
            {
                s.SwaggerEndpoint("/swagger/v1/swagger.json", "MVR.Api.Jobs");
            });
        }

        ConfigureExceptionHandling(app);
        app.UseMvc();
        app.UseCors("MVRCors");

        loggerFactory.AddSerilog();
    }
like image 982
willthiswork89 Avatar asked Dec 18 '18 17:12

willthiswork89


People also ask

How do I fix HTTP 415 unsupported media type?

Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

Why do we get 415 error?

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.

What is FromBody ASP NET core?

[FromBody] attributeThe ASP.NET Core runtime delegates the responsibility of reading the body to an input formatter. Input formatters are explained later in this article. When [FromBody] is applied to a complex type parameter, any binding source attributes applied to its properties are ignored.


2 Answers

Try to replace [FromQuery] to [FromForm] in your controller.

like image 93
Roman Himadinov Avatar answered Sep 18 '22 11:09

Roman Himadinov


It is a known issue with 2.2

https://github.com/aspnet/AspNetCore/issues/4396

It appears that this bug fix in 2.2 caused any GET requests to honour the [Consumes] attribute on a controller. Previously, in 2.1, they did not.

The workaround is to remove the [Consumes] attribute from the controller and apply it only to non-GET methods in your controller, or downgrade and keep using .NET Core 2.1 until they release a fix.

It has already been fixed for the 3.0 .NET Core release. I think they are still deciding if they will fix it in a 2.2 service release.

like image 44
Simply Ged Avatar answered Sep 19 '22 11:09

Simply Ged