30

How to use POSTMAN for Multipart/form-data which has customize header for testing my controller which takes 2 files as parameter (public ... controller( MultipartFile[] files))?

POST .... HTTP/1.1
.
.
.
---boundary123
Content-type:application/octet-stream
content-Disposition: form-data filenale="abc.txt" name="someuniquename"
[paylaod content](this is in xml format)
---boundary123
content-type:application/json
content-Disposition:form-data name="metadata"
{ID:"999"}
---boundary123
Afridi
  • 6,753
  • 2
  • 18
  • 27
phalco
  • 357
  • 2
  • 5
  • 12

4 Answers4

65

enter image description here

Steps to use 'Multipart/form-data ' in Postman

  1. Create a new tab
  2. Insert controller Url
  3. Set method type as POST
  4. Under Body tab, select form-data
  5. For each key that is a file, set Value type as File
Afridi
  • 6,753
  • 2
  • 18
  • 27
  • 2
    Do I need to enter customize headers and boundary?? – phalco May 25 '17 at 14:43
  • @phalco what type of customize header? for content type? – Afridi May 25 '17 at 14:47
  • it contains host, content type(multipart/form-data), boundary, payload count also in header – phalco May 25 '17 at 14:52
  • you can add your custom headers as well. But as for content type is concerned, there is no need. host is already included in your url. payload count etc its totally depends on your requirements. But no need for default implementations. – Afridi May 25 '17 at 14:58
  • Yes sure, I am trying to fix some errors in my code. – phalco May 25 '17 at 16:04
9

I hope this will help others avoid long debugging efforts. Bottom line is that for some multipart uploads, you're just flat out of luck. For instance, if you need to do multipart/related, and need to express that in the Headers with a Content-Type, Postman can't help you. Primarily that's because Postman ONLY generates a random boundary, even if you add your own. The difficult part is that Postman will claim to be using your boundary in the Postman Console, but will actually be using a different boundary in the call. Thus the header boundary declared and the boundary actually used won't match.

Here's an example of a request from Postman, viewed both in the Postman Console and also in Fiddler. As you can see, Fiddler shows Postman is actually sending a random boundary, where Postman is claiming to use the provided boundary.

Fiddler vs Postman rendering of raw request

I really hope they fix that in Postman. At least show it the Postman Console, even if they don't fix the underlying issue. It's a great tool for most APIs, but if you're trying to hit a DICOM server, and being compliant about it... you're out of luck.

Steven Borg
  • 641
  • 1
  • 6
  • 14
3

This is a long-known issue for Postman. It can be a bit tricky if you have a set up that involves using say text or JSON for one part but say a picture for another. The key is to set the Content-Type Header to multipart/mixed and then to convert everything into a file. You can ignore the "convert it into a file" step if it's text :)

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. Hope that helps someone!

Adam Gerard
  • 708
  • 2
  • 8
  • 23
-2

This video describes how to post multipart/form-data using postman. I uploaded a text file with Content-Type of multipart/form-data.

Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
  • Can you also add a description of the required steps to your answer? Links can be corrupted and/or disabled so the information will be lost. – pitprok Jul 19 '22 at 10:49