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

Viewed 21684

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();
    }
3 Answers

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.

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

Set the content type in postman to:

Content-Type: application/json
Related