49

I'm struggling with creating POST multipart/mixed request with Postman Chrome extension

Here is my curl request what works nice

curl -H "Content-Type: multipart/mixed" 
-F "metadata=@simple_json.json; type=application/json "
-F "content=@1.jpg; type=image/jpg" -X POST http://my/api/item -i -v

interesting part of response

Content-Length: 41557

Expect: 100-continue

Content-Type: multipart/mixed; boundary=----------------------------8aaca457e117

  • additional stuff not fine transfer.c:1037: 0 0
  • HTTP 1.1 or later with persistent connection, pipelining supported

And when I use Postman enter image description here

I getting such response

{"message":"Could not parse multipart servlet request;
 nested exception is org.apache.commons.fileupload.FileUploadException: 
 the request was rejected because no multipart boundary was     
 found","type":"error","status":500,"requestId":"1861eloo6fpio"}

That's it - I wish to get rid of that error. If some more information needed please ask :)

Santanu Dey
  • 2,900
  • 3
  • 24
  • 38
pbaranski
  • 22,778
  • 19
  • 100
  • 117

2 Answers2

87

I was facing this problem too. Short answer: remove the Content-Type header from your Postman request.

The long story is that the Content-Type for a multipart request should be rather special -- it should look kind of like this:

multipart/form-data; boundary=----WebKitFormBoundaryzeZR8KqAYJyI2jPL

The problem is that the boundary is important and it needs to exactly match the boundary used to separate the files being uploaded. The solution is simple: do not specify a Content-Type! When you upload files, Postman will automatically append the above content type for you, except the boundary will be filled in with whatever Postman or Chrome is using to separate the multipart content.

You can verify this behavior by using Chrome developer tools (within Postman) to examine the Content-Type header being added, in addition to the Content-Disposition headers of the multipart data, which are also a pain to construct manually (and impossible within Postman).

Note: My answer is a solution for those who need a multipart/form-data answer. The OP was looking for a multipart/mixed solution. My answer will not suffice in this scenario. That being said, it seems a lot of people just want the multipart/form-data solution, so I will leave my answer here.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • 4
    Tried removing content type, but doesn't works. The curl request works but fails when tried from POSTMAN – Aditya Apr 05 '16 at 07:01
  • @Aditya, it works for at least 33 other people. :) Maybe you'd be best off asking a new question, so you can post your headers, response, etc. (feel free to reference this question as well) Or you can hit me up on Twitter (see my profile). – Kirk Woll Apr 06 '16 at 13:56
  • Added new question, http://stackoverflow.com/questions/36540719/sending-multipart-form-data-content-with-postman-chrome-extension – Aditya Apr 11 '16 at 05:50
  • 7
    When the explicit header is removed, Postman includes `multipart/form-data`, which is not the same as `multipart/mixed`. – wst May 18 '16 at 14:16
  • @wst your criticism is fair, and I've modified my answer to better reflect its inadequacy. That being said, it still seems useful to many, so I will leave it here for others. – Kirk Woll Aug 17 '16 at 21:47
1

Left this comment on: https://github.com/postmanlabs/postman-app-support/issues/1104

Ninja update: Not sure if this will help anyone else but there is a workaround for a specific scenario where you have multiple file types / content types being uploaded in a single multipart POST request.

  1. Set the Header Content-Type to multipart/mixed.
  2. Select the form-data option in Body.
  3. Convert all of your items into files. String content should become a text file, etc.
  4. Add each file by selecting file, adding a key name.

This approach doesn't require actually manually specifying each Content-Type or Content-Disposition. The trick here was to serialize all relevant content into a persistent file type. You can ignore the "convert it into a file" step if it's text :) Hope that helps someone!

Adam Gerard
  • 708
  • 2
  • 8
  • 23
  • Perfect..Thanks this is working..I was spending lot time on how to send an multipart/mixed encrypted zip file as an attachment – Ratha Nov 20 '20 at 06:09