Unsupported Media Type 415 in ASP.NET Core Web API

In this article, we will create a simulation to get a HTTP 415 "Unsupported Media Type" error.


- Create a new project


Http415Code-1.PNG

- Select ASP.NET Core Web Application


Http415Code-2.PNG

- Name the project


Http415Code-3.PNG

In the Create a new ASP.NET Core Web Application dialog, confirm that .NET Core and ASP.NET Core 3.1 are selected.


- Select the API template. Click Create.


Http415Code-4.PNG

In this way, you will create a new ASP.NET Core Web API Application


Create a new folder named Entities and then create a new class named Customer in the Entities folder as shown below.


using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Entities
{
    public class Customer
    {
        [Key]
        public int ID { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }

        public string Address { get; set; }
    }
}

- Add a new API controller named CustomerController in the Controllers folder.


Http415Code-5.PNG

- Add the following code snippet to the CustomerController.cs file


using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using WebApplication1.Entities;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CustomerController : ControllerBase
    {
        private static List<Customer> customers = new List<Customer>
        {
            new Customer { ID = 1, FirstName = "Judy", LastName = "Fortensky", Address = "Pennsylvania" },
            new Customer { ID = 2, FirstName = "Sara", LastName = "Garland", Address = "San Francisco" },
            new Customer { ID = 3, FirstName = "Gene", LastName = "Taylor", Address = "San Francisco" },
            new Customer { ID = 4, FirstName = "Larry", LastName = "Kelly", Address = "San Francisco" }
        };

        [HttpPost]
        public IActionResult Post([FromBody] Customer customer)
        {
            int pID = customers[customers.Count - 1].ID + 1;
            customer.ID = pID;
            customers.Add(customer);

            return CreatedAtRoute("CustomerById", new { id = customer.ID }, customer);
        }

        [HttpGet("{id}", Name = "CustomerById")]
        public IActionResult Get(int id)
        {
            Customer customer = customers.Find(x => x.ID == id);
            return Ok(customer);
        }
    }
}

- Start the project ans test the project with POSTMAN


After running the application, we will get a screenshot as follows


Http415Code-6.PNG

415 Unsupported Media Type


In the body request, we are sending an invalid JSON. We don't set content type right in the header.


To solve it, Click in Text and select the JSON option from the drop-down list. Then add the open and closed braces as shown below.


Http415Code-7.PNG

We received an HTTP 400 code. Because we didn't submit a customer object to the API.


Now let's see what happens after submitting the customer object


{
    "FirstName":"Tom",
    "LastName":"Anderson",
    "Address":"Boston"
}

Http415Code-8.PNG

We received an HTTP 201 code. It works fine.


Download the source code.