Web Api 2.1 ReadAsMultipartAsync

Topics: ASP.NET Web API
Jan 25, 2014 at 9:56 AM
I'm using AJAX to post multi part form data to Web Api. Prior to 2.1, everything worked fine.
My code is bombing when it calls: await Request.Content.ReadAsMultipartAsync(provider);

Is there an issue with ReadAsMultipartAsync?



Accept application/json, text/plain, /
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 272
Content-Type multipart/form-data; boundary=---------------------------265001916915724

-----------------------------265001916915724 Content-Disposition: form-data; name="value" {"Name":"T-Mobile","Picture":"","DisplayOrder":1,"Published":true,"Deleted":false,"Id":2,"CustomProperties":{},"editMode":false} -----------------------------265001916915724--



Error:

"Message":"An error has occurred.","ExceptionMessage":"Error reading MIME multipart body part.","ExceptionType":"System.IO.IOException","StackTrace":" at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0`1.MoveNext()\r\n---
Developer
Jan 25, 2014 at 5:49 PM

Can you paste the actual format of the request so that we can try to repro the issue? Currently when I copy paste the below request I see that the line breaks (CRLF) are not in the expected places. Looks like you copied this request content from a browser’s debugging utility. You can use a tool like Fiddler to capture the request and send us the details. Thanks.

Jan 25, 2014 at 7:08 PM
Edited Jan 25, 2014 at 7:14 PM
Hi. Thank you for your quick reply. Here is the request from Fiddler

Host: localhost:9876
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: application/json, text/plain, /
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:9876/admin/users Content-Length: 321 Content-Type: multipart/form-data; boundary=---------------------------41184676334 Cookie: _ga=GA1.2.1724244515.1386066368; __RequestVerificationToken=-HGrrBxFml4eR0cxPAXEGthzf2LIA0YcYMAC4LmtHoQbjAPUFlTDI0QmJleLvVOkujkUBqzremKGLVN3fPsT34XYWkE1; PROJECTC.AUTH=06910EEFA65C02FE7FC30AABCF22EAA18354B0C386A8017B62CDD4302DAC62B0C9CA0D79926E5CE8836CC65EDC07CFA606E644E399D59EAC4213C31B0BFFEE8A8C0908166A43F65312317DD6DE7645E46AE6D75E58F8786A23D0862B40C48DA18B19EFC246FF1CC143B6644E597E3FF52DDEA36F; ProjectC.User=1
Connection: keep-alive

-----------------------------41184676334
Content-Disposition: form-data; name="value"

{"Name":"Sprint","Picture":"http://localhost:9876/datastore/images/sprint-logo_25_h.png","DisplayOrder":0,"Published":true,"Deleted":false,"Id":1,"CustomProperties":{},"editMode":false}
-----------------------------41184676334--
Developer
Jan 25, 2014 at 8:31 PM

Thanks. I am unable to repro your scenario with the given request.

Could you share a standalone repro so that we can dig deeper into the issue?

Also I see that you are sending Json data as form-data. Ideally you should also be sending Content-Type in the subpart to be application/json so that you can deserliaze it later.

Jan 25, 2014 at 8:42 PM
Ok, I'll put a repo together for you.

My request may also include a file upload, but in the example I sent you, I did not select a file to upload. That's why I am using multi part form data for this request.
Jan 25, 2014 at 11:18 PM
HI kichalla,

When I made a new MVC5.1 and Web API 2.1 project based off the my code, await Request.Content.ReadAsMultipartAsync(provider)
worked as expected.

So I went back to my project and took a look at the Nuget packages. I removed Glimpse MVC 4.0, updated all my DotNetOpenAuth packages (just to be safe), and changed my site's target RunTime from 4.5 to 4.5.1

Everything is working now, although I am not sure which binaries was causing a conflict or such that would make ReadAsMultipartAsync not originally work.

Thank you and sorry for the false alarm.