WebAPI cannot parse multipart/form-data post

[Origin]: http://education-eco.blogspot.ca/2014/01/webapi-cannot-parse-multipartform-data.html

I’m trying to accept a post from a client (iOS app) and my code keeps failing on reading the stream. Says the message is not complete. I’ve been trying to get this working for hours it seems like something is wrong in my message format or something. All I’m trying to do is read a string but the developer I’m working with who is doing the iOS part only knows how to send multipart/form-data not content-type json.

Here is exact error: Unexpected end of MIME multipart stream. MIME multipart message is not complete.”

It fails here: await Request.Content.ReadAsMultipartAsync(provider);


POST http://localhost:8603/api/login HTTP/1.1
Host: localhost:8603
Accept-Encoding: gzip,deflate
Content-Type: multipart/form-data; boundary=------------nx-oauth216807
Content-Length: 364
Accept-Language: en-us
Accept: */*
Connection: keep-alive


Content-Disposition: form-data; name="token"


Here is the WebAPI code:

    public async Task<HttpResponseMessage> PostFormData()
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        // Read the form data and return an async task.
        await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
            Trace.WriteLine("Server file path: " + file.LocalFileName);
        return Request.CreateResponse(HttpStatusCode.OK);

        catch (System.Exception e)
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);

A \r\n needs to be appended at the end of request content stream.

Instead of using this line to read the data: await Request.Content.ReadAsMultipartAsync(provider);

You will need to:
1. load the request stream to memory
2. append the \r\n string that is required
3. create a stream content from the memory content
4. manually add the request headers to the stream content
5. Finally use this instead: streamContent.ReadAsMultipartAsync(provider);

Check the answer of Landuber Kassa here for the complete code: http://stackoverflow.com/questions/13770536/asp-net-web-api-unexpected-end-of-mime-multi-part-stream-when-uploading-from-fl/17290999#comment42617381_17290999


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s