Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter

I'm writing a web api that have a post method accepting files from uploaded from UI.

public async Task<List<string>> PostAsync()
    {

        if (Request.Content.IsMimeMultipartContent("form-data"))
        {
            string uploadPath = HttpContext.Current.Server.MapPath("~/uploads");

            var streamProvider = new MyStreamProvider(uploadPath);

            await Request.Content.ReadAsMultipartAsync(streamProvider);

            return streamProvider.FileData
                .Select(file => new FileInfo(file.LocalFileName))
                .Select(fi => "File uploaded as " + fi.FullName + " (" + fi.Length + " bytes)")
                .ToList();
        }
        else
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!");
            throw new HttpResponseException(response);
        }
    }

then i post a request to above action by postman. I set content-type header to multipart/form-data but an error occures during execution of action. here is error message body :

"Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.\r\nParameter name: content"

i went to the postman headers but i found that request header content-type in was set to application-json.

Postman screenshot

can anyone help me?

like image 787
Hamid Avatar asked Jul 01 '14 06:07

Hamid


1 Answers

You are looking on the response header which is json format and this is ok for you.

Your real problem is with the postman request, so just remove the 'Content-Type: multipart/form-data' entry from request header. It's enough to upload a file as form-data and send the request.

Look what happen when you set the Content-Type manually vs. when you not: enter image description here enter image description here

Postman knows to set both the content type and boundary, since you set only the content type

like image 198
Popa Andrei Avatar answered Sep 30 '22 07:09

Popa Andrei