Coder Perfect

A HTTP 415 Unsupported Media Type response is returned while using ASP.NET Core form POST.

Problem

Sending a form POST HTTP request to the below controller (Content-Type: application/x-www-form-urlencoded) results in an HTTP 415 Unsupported Media Type response.

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

HTTP headers for form submission:

POST /submit HTTP/1.1
Host: example.com:1337
Connection: keep-alive
Content-Length: 219
Pragma: no-cache
Cache-Control: no-cache
Origin: https://example.com:1337
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: https://example.com:1337/submit
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,nl;q=0.6

On.NET 4.6, this worked with ASP.NET MVC 5.

Asked by Bart Verkoeijen

Solution #1

Instead of using the [FromBody] attribute for forms, use the [FromForm] attribute.

The controller below is compatible with ASP.NET Core 1.1:

public class MyController : Controller
{
    [HttpPost]
    public async Task<IActionResult> Submit([FromForm] MyModel model)
    {
        //...
    }
}

If your controller is tagged with [ApiController], [FromXxx] is necessary. It can be omitted for regular view controllers.

Answered by Bart Verkoeijen

Solution #2

You can use [FromBody], but you must set your request’s Content-Type header to application/json, i.e.

Content-Type: application/json

Answered by Bjorn Bailleul

Solution #3

To begin, you must define the Content-Type in the Headers, for example, application/json.

You must send a json if the content type is set to application/json.

So instead of form-data or x-www-for-urlencoded, send raw json, such as “Username”: “user”, “Password”: “pass” in the body of your request.

You can change the sample to fit different types of content, such as what you wish to send.

To experiment with this, you can use a tool like Postman or curl.

Answered by Gabriel P.

Solution #4

You don’t have to utilize [FromForm] to retrieve form data in the controller, in addition to the good replies. Framework converts form data to model according to your preferences. You can implement like following.

[HttpPost]
public async Task<IActionResult> Submit(MyModel model)
{
    //...
}

Answered by hasan

Solution #5

This is my situation: It’s being executed in the following environment: AspNet Core 2.1 Controller:

public class MyController
{
    // ...

    [HttpPost]
    public ViewResult Search([FromForm]MySearchModel searchModel)
    {
        // ...
        return View("Index", viewmodel);
    }
}

View:

<form method="post" asp-controller="MyController" asp-action="Search">
    <input name="MySearchModelProperty" id="MySearchModelProperty" />
    <input type="submit" value="Search" />
</form>

Answered by Quang Vu

Post is based on https://stackoverflow.com/questions/44538772/asp-net-core-form-post-results-in-a-http-415-unsupported-media-type-response