Search code examples
c#postmanasp.net-core-webapiasp.net-core-2.2http-status-code-415

asp.net core 2.2 posting file results in http/415 unsopported mediatype


From postman I'm trying to post a file to an asp.net core 2.2 api-controller.

POST /api/epd/noshow/uploadfile HTTP/1.1
Host: localhost:44361
User-Agent: PostmanRuntime/7.11.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 3d633228-2554-442b-84f4-4e8214972886,a8f994f4-973e-411f-b96b-778af2c082b4
Host: localhost:44361
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------201375848683546790642901
content-length: 237
Connection: keep-alive
cache-control: no-cache
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW


Content-Disposition: form-data; name="file"; filename="C:\temp\caldr.json


------WebKitFormBoundary7MA4YWxkTrZu0gW--

The api-controller method

[HttpPost]
[AllowAnonymous]
public IActionResult UploadFile([FromForm]IFormFile file)
{
    return Ok();
}

I've already tried FromForm, FromBody, nothing. Setting content-type in postman, not setting the content-type in postman always returns the same error.

Any suggestions ?


Solution

  • Found the problem and solution. Just for reference if someone else encounters the same mind boggling problem.

    On the 'grandmother' class of the controller, there is the following attribute :

    [Consumes("application/json")]
    

    of course form-data was not accepted :-)

    override this default behaviour by adding the following attribute to the action

    [Consumes("multipart/form-data")]