Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

415 Unsupported Media Type asp.net core

Tags:

Detail

I am trying to post a file from Postman to the endpoint I have created. but it gives me this error. I am not passing the header Content-Type in postman

415 Unsupported Media Type

API

[Consumes("multipart/form-data")]
[HttpPost]
public async Task<IActionResult> SendEmail([FromBody]Entity entity)
{
    try
    {

        return OK();
    }
    catch (Exception e)
    {
        throw e;
    }
}

Class

public class Entity 
{
    public List<IFormFile> Files { get; set; }
}
like image 805
Malik Kashmiri Avatar asked Mar 12 '18 05:03

Malik Kashmiri


People also ask

How do I fix 415 unsupported media type?

Fixing 415 Unsupported Media Type errors Ensure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

What is difference between FromForm and FromBody?

[FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

Why do we get 415 error?

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.

What is IFormFile C#?

What is IFormFile. ASP.NET Core has introduced an IFormFile interface that represents transmitted files in an HTTP request. The interface gives us access to metadata like ContentDisposition, ContentType, Length, FileName, and more. IFormFile also provides some methods used to store files.


1 Answers

Try using [FromForm] instead of [FromBody] for the method parameter if you're POSTing form data.

like image 142
Dom Avatar answered Sep 25 '22 14:09

Dom