489

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

401

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
The hard part is understand why it's called multipart request, instead of something more obvious, like file upload request.
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.
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
More details with firebug screenshots here: cubicrace.com/2016/05/upload-files-https-using-java.html
|
52

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 Comments

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.
@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".
4

I have found an excellent and relatively short explanation here.

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

2 Comments

Not necessarily REST request.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.