ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response
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.
415 Unsupported Media Type in ASP.NET core web api
Which version of .NET Core are you using?
Try doing the request from the browser and see if you have the same result.
Also, are you sure you're doing a GET and not a POST request in Postman? You shouldn't get 415 errors for GET requests, especially when you're not sending any body.
This error mainly occurs when you try to send a body and you haven't specified the media-type through the Content-Type
header.
Ensure that the request is GET and your body is empty.
Solution after post edit:
As you're trying to parse a DTO object(SomeClassObj
), you should specify where the values should come from. In order to fix your specific case, add the [FromQuery]
attribute before SomeClassObj
.
Your code should look like this:
[ApiController]
[Route("MyController")]
public class MyController : ControllerBase
{
[HttpGet]
[Route("GetResult")]
public IActionResult GetResult(string param1, string param2= null, [FromQuery]SomeClassObj obj = null)
{ .... }
}
This tells the parser to fetch the data from the query string. This will fix the 415
issue. However, if you want to bind to complex types, especially on get, checkout those topics: ASP.NET CORE 3.1 Model Binding and this issue as you will most probably encounter issues with parsing your DTO object.
415 Unsupported Media Type asp.net core
Try using [FromForm]
instead of [FromBody]
for the method parameter if you're POSTing form data.
"Unsupported Media Type", 415 eror when post request in react-native
The problem is in the parameters of fetch. fetch doesn't have any parameter named header
but it's headers
with an s. So, your code wasn't asking for the correct Content-type and Accept Headers. To fix this just add an s to the header key :
fetch('http:/.../api/login/register/',{
method:'post',
headers:{
'Accept': 'application/json',
// 'Content-type': 'application/json'
'Content-Type': 'application/json; charset=utf-8'
},
body:JSON.stringify({
'kullaniciAdi': userName,
'sifre': userPassword
})
})
.then((response) => response.json())
.then((responseJson)=>{
console.log(Object.values(responseJson));
Unfortunately, Javascript doesn't highlight this error.
Error 415 while uploading a file through Postman to ASP.NET Core API
If you want to receive IFormFile
, you need post content-type=multiple/form-data
which is from form data instead of body. Also remember to change [FromBody]
to [FromForm]
.
Postman:
Controller:
[HttpPost]
[Route("{id}")]
public async Task<IActionResult> PostImage([FromForm] IFormFile file, [FromRoute] int id)
{}
If you post file from body, maybe you post a byte array. If though you need change IFormFile file
to byte[] file
.
Related Topics
Create a C# Method to Generate Auto Increment Id
Empty String Not Being Converted to Null When Passing Json Object to Controller
Angular Post Request Received in Net Core API as Null
How to Get the Currently Loggedin Windows Account from an ASP.NET Page
How to Extract Custom Header Value in Web API Message Handler
Deserialize Json Array to C# List Object
Deserialize Json String in to Multiple C# Objects
How to Properly Make a Http Web Get Request
How to Download File With ASP.NET on Buttton'S Onclick Event
Fast Way of Finding Most and Least Significant Bit Set in a 64-Bit Integer
Access Variables/Functions from Another Component
How to Ignore Convert to Nullable in Lambda Expressions
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
Get Value from Jtoken That May Not Exist (Best Practices)
How to Select Last Record in a Linq Groupby Clause