Search code examples
asp.nethttpasp.net-web-apiasp.net-web-api2mime

POSTing MIME multipart content to a Web Api 2


I am unable to POST a multipart/form-data request to my Web Api 2.

The HTTP request:

POST /api/v1/software/947ee15c-0117-47d2-c567-a435010d18f3/file HTTP/1.1
Host: localhost:4022
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Accept: application/json
Cache-Control: no-cache
Postman-Token: 7aca3e0b-cc82-7ced-b419-5b1cff9462a8

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="code.png"
Content-Type: image/png


----WebKitFormBoundary7MA4YWxkTrZu0gW

The controller action:

[Route("{id:Guid}/file")]
public IHttpActionResult PostFile()
{
    if (Request.Content.IsMimeMultipartContent())
    {
        // Upload the file!
    }
    else
    {
        return BadRequest("Unsupported media type");
    }
}

The action always responds with the bad request.

Request.Content.IsMimeMultipartContent() is always false.

What am I doing wrong?


Solution

  • I fixed the problem by using the following Accept header value:

    text/html,image/png,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8