451

I have been writing iPhone applications for some time now, sending data to server, receiving data (via HTTP protocol), without thinking too much about it. Mostly I am theoretically familiar with process, but the part I am not so familiar is HTTP multipart request. I know its basic structure, but the core of it eludes me.

It seems that whenever I am sending something different than plain text (like photos, music), I have to use a multipart request. Can someone briefly explain to me why it is used and what are its advantages?

If I use it, why is it better way to send photos that way?

titusn
  • 1,201
  • 1
  • 12
  • 43
MegaManX
  • 8,766
  • 12
  • 51
  • 83
  • 3
    See the following link for information: [http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2) – zargarf Jun 06 '13 at 09:35

3 Answers3

382

An HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.

Cypress Frankenfeld
  • 2,317
  • 2
  • 28
  • 40
Iggy
  • 8,463
  • 3
  • 31
  • 21
  • 6
    Just wanted to add that _multipart form data fields are sent in order_. This is not something that's immediately obvious-- I added a list of links here: https://github.com/balderdashy/skipper/blob/master/README.md#the-big-assumption-field-order If I get some time to put together a test case using PhantomJS/webkit, I'll add the link there as well. Browsers obey this part of the spec, even as far back as IE6. – mikermcneil Aug 03 '14 at 23:20
  • 130
    The hard part is understand why it's called *multipart request*, instead of something more obvious, like *file upload request*. – Rafael Eyng Mar 19 '15 at 12:19
  • 50
    The OP wanted a philosophical approach and answer. This answer is not explaining "why" part. It's more about "what" part. I'm not a big fan of downvoting, but I argue that this answer is not what OP wanted and I searched for. – Saeed Neamati Aug 19 '15 at 07:47
  • I've been thinking for a while now: Wouldn't multipart requests be a way to reduce the number of connections required for a website. Even just sending all of the JavaScript files or all of the CSS files together in one request would (perhaps?) provide benefits. I suppose now though, with HTTP/2.0, the point is moot. – Richard Sep 13 '15 at 20:27
  • 10
    The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data. ORIGINAL SOURCE - http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 – Aditya Aggarwal Nov 05 '15 at 07:55
  • 3
    More details with firebug screenshots here: http://www.cubicrace.com/2016/05/upload-files-https-using-java.html – Piyush Chordia May 02 '16 at 07:13
  • Are there issues with appending extra data to the multipart request? Ie: Appending json data from the front end to the multipart request. – Steven Nov 18 '16 at 20:01
  • I have developed a proxy server and would like to monitor any file upload\download via https\http requests. Is there any specific parameter in the request using which I can conclusively say the given request is a upload\download? – Jude Aloysius Sep 14 '17 at 07:10
  • 1
    Is it possible to preempt the full receipt of a multipart request. Say I am sending a large payload and I am sending a little bit of metadata about that payload so the server can perform some validations and fail the request sooner if some conditions are not met. Currently, I receive the full request and then perform the validation, but it seems extraneous to receive and store the payload even temporarily if the request ultimately must be _failed_ by the server. I can send the metadata at the start of the request followed by the payload. – Web User Jun 20 '18 at 18:10
  • Steven, there are no real issues with that - in fact, that's why it is a *multipart* request. You can add as many parts as you need. Think of an "usual" HTTP form: there is a list of fields, and all of them are sent in a single multipart POST. – foo Jun 28 '19 at 13:28
  • For uploading the media content we use base64, and when we fetch the media from the server the API returns an URL to the image which we then use it in our views to load the image with some image caching for performance. So far we have not used multipart/forms for uploading images but the internet is full of answers including multipart form data so I was hoping if there was some solid technical reason as to why should one be using multipart form data as compared to base64 or byte array approach. – Bug Hunter Zoro Apr 11 '20 at 16:17
50

As the official specification says, "one or more different sets of data are combined in a single body". So when photos and music are handled as multipart messages as mentioned in the question, probably there is some plain text metadata associated as well, thus making the request containing different types of data (binary, text), which implies the usage of multipart.

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
csonti
  • 515
  • 4
  • 5
  • 8
    I don't think that's the case. When uploading an image, the whole image (including metadata) will be *one* set of data in the request body. It's still a multipart request, even if there's just one part in the body. You can also create a request to upload multiple files at once. – Dario Seidl Sep 29 '18 at 11:48
  • 9
    @DarioSeidl the standard assumes you may be submitting a file upload from a webform, which can include other data fields in addition to the file upload itself. For instance, in addition to the original file name, the user might include a description. Multipart also handles generic binary blobs that are disconnected from the concept of a particular originating "file". – Ionoclast Brigham Nov 15 '18 at 00:57
2

I have found an excellent and relatively short explanation here.

A multipart request is a request containing several packed requests inside its entity.

Jakov
  • 879
  • 2
  • 17
  • 36
  • 5
    Not necessarily REST request. – Yaroslav Shabalin Mar 15 '22 at 15:01
  • This link contains the documentation of some SAP's ABAP library that features `sending multiple fully qualified HTTP requests/responses`. This is not a multipart request in its most common meaning which I believe is a request with `Content-Type: multipart/form-data` – starteleport Apr 14 '23 at 11:43