ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response
First you need to specify in the Headers the Content-Type
, for example, it can be application/json
.
If you set application/json
content type, then you need to send a json.
So in the body
of your request you will send not form-data
, not x-www-for-urlencoded
but a raw
json, for example {"Username": "user", "Password": "pass"}
You can adapt the example to various content types, including what you want to send.
You can use a tool like Postman or curl to play with this.
You can use [FromBody]
but you need to set the Content-Type
header of your request to application/json
, i.e.
Content-Type: application/json
For forms, use the [FromForm]
attribute instead of the [FromBody]
attribute.
The below controller works with ASP.NET Core 1.1:
public class MyController : Controller
{
[HttpPost]
public async Task<IActionResult> Submit([FromForm] MyModel model)
{
//...
}
}
Note: [FromXxx]
is required if your controller is annotated with [ApiController]
. For normal view controllers it can be omitted.