457

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?

1

3 Answers 3

385

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.

11
  • 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: github.com/balderdashy/skipper/blob/master/… 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. Aug 3, 2014 at 23:20
  • 134
    The hard part is understand why it's called multipart request, instead of something more obvious, like file upload request. Mar 19, 2015 at 12:19
  • 51
    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. Aug 19, 2015 at 7:47
  • 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 - w3.org/TR/html401/interact/forms.html#h-17.13.4.2 Nov 5, 2015 at 7:55
  • 3
    More details with firebug screenshots here: cubicrace.com/2016/05/upload-files-https-using-java.html May 2, 2016 at 7:13
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.

2
  • 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. Sep 29, 2018 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". Nov 15, 2018 at 0: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.

2
  • 5
    Not necessarily REST request. Mar 15, 2022 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 Apr 14 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.