16

I'm writing a web API that has a post method accepting files 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 for the above action by postman. I set the content-type header to multipart/form-data but an error occurred during the execution of action. here is the 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 the request header content type was set to application-json.

Postman screenshot

spaleet
  • 838
  • 2
  • 10
  • 23
Hamid
  • 962
  • 3
  • 9
  • 21
  • 1
    I'm not sure I understand. The Content-Type header with the "?" next to it is the response header (not the request header, as you say), which looks ok since the response you got was an error in json format. The problem must be elsewhere, specifically in the way you submit your data in the request. – djikay Jul 02 '14 at 10:40
  • Thank you for your reply.You are right.It's the response header.I realized my mistake and the problem is somewhere else... – Hamid Jul 02 '14 at 11:16
  • You are welcome. I just hope it helped you a little. – djikay Jul 02 '14 at 11:30
  • 2
    I found it! there is a request header attribute named "enctype" that must be set to "multipart/form-data" to make upload work. thank alot for your help – Hamid Jul 02 '14 at 11:46
  • Ah, yes. If you haven't read it, take a look at [this protected SO question](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) for more information about `enctype`. – djikay Jul 02 '14 at 12:26

2 Answers2

16

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

Popa Andrei
  • 2,299
  • 21
  • 25
  • If someone is looking for visual details on how *not* to do it in postman. Please check this [answer](https://stackoverflow.com/a/41435972/465053). – RBT Dec 01 '17 at 09:55
0

First: Postman have a bug in handling file-based requests.

You can try adding this to your WebApiConfig.cs it worked for me:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45