POSTMAN for Multipart/form-data
Asked Answered
F

5

32

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
Faggot answered 25/5, 2017 at 14:13 Comment(1)
At last! Apparently this has now been fixed: github.com/postmanlabs/postman-app-support/issues/1104Sulfaguanidine
R
67

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
Rikkiriksdag answered 25/5, 2017 at 14:22 Comment(6)
Do I need to enter customize headers and boundary??Faggot
@Faggot what type of customize header? for content type?Rikkiriksdag
it contains host, content type(multipart/form-data), boundary, payload count also in headerFaggot
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.Rikkiriksdag
Yes sure, I am trying to fix some errors in my code.Faggot
It seems necessary to set the 'boundary' for some payloadsAntefix
B
10

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.

Baer answered 22/7, 2020 at 21:38 Comment(1)
@postman team, thoughts?Roeder
S
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!

Sulfaguanidine answered 10/1, 2019 at 20:30 Comment(0)
P
0

for what it's worth, because I wasted a couple of hours on this :

I have an endpoint that will accept multipart, with one part being either image/* or audio/*.

The openApi I imported in Postman was like this :

  encoding:
    asset_file:
      contentType: image/*, audio/*

The problem is that I was trying to upload an audio and the server checks the value of the contentType received, depending on what it starts with.

The problem in Postman is that the Content-Type column is hidden by default

enter image description here

It took me a while to realize it and then display it so that I could modify it myself

enter image description here

Pangaro answered 10/5 at 19:13 Comment(0)
L
-5

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

Levee answered 2/5, 2020 at 10:10 Comment(1)
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.Petition

© 2022 - 2024 — McMap. All rights reserved.