What is http multipart request?
Asked Answered
L

3

466

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?

Lydell answered 6/6, 2013 at 9:28 Comment(1)
See the following link for information: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2Enochenol
B
388

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.

Blancablanch answered 31/10, 2013 at 16:38 Comment(11)
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.Matthia
The hard part is understand why it's called multipart request, instead of something more obvious, like file upload request.Palecek
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.Slifka
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.Connecticut
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.2Snappy
More details with firebug screenshots here: cubicrace.com/2016/05/upload-files-https-using-java.htmlJulieannjulien
Are there issues with appending extra data to the multipart request? Ie: Appending json data from the front end to the multipart request.Elenor
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?Katonah
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.Avra
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.Arteritis
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.Leasia
I
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.

Immorality answered 17/2, 2017 at 15:40 Comment(2)
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.Brede
@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".Inexplicable
A
2

I have found an excellent and relatively short explanation here.

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

Anecdote answered 24/12, 2020 at 14:6 Comment(2)
Not necessarily REST request.Harwin
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-dataTakahashi

© 2022 - 2024 — McMap. All rights reserved.