Context
I have developed a Web Api using .Net technology and one of the actions is to upload image files.
The API will receive requests from mobile applications (Android and iOS).
The API must meet one of these two conditions:
- Upload a file for each request using
multipart/form-data
, that is, the file travels directly embedded in the request - Upload a file for each request by sending it in the body as
string
encoded inbase64
Both actions work and receive the file (it is handled depending on the format the API receives it in).
Ask
Is there an advantage or best practice to using multipart/form-data
instead of base64
under the exposed scenario?
base64
is the conversion of bytes to ASCII characters . It is not recommended that you use base64 as this adds more weight to the request. For example, the simple characterhola
that is 4 bytes converted to base64 makes 8 bytes (hello, in base64 it isaG9sYQ==
) and as you just said that it will be an android application that will send the data, you would be consuming more internet data unnecessarily to the users.Another thing to note is that base64 has to be decoded on the server adding this plus processing. If you send a file of +10MB you would have to first:
In the case of multipart, this is part of the HTTP protocol while base64 is not. Although it does not compress them, it ensures compatibility with all http servers and sends any type of file.
Advantage:
base64
It is a way of coding that enlarges the data up to 30%,multipart/form-data
it is part of the standardhttp
and allows sending binary data, because of that itmultipart
is faster and consumes less bandwidthbase64
converts binary data to a "printable" ASCII representation, the 8-bit space is reduced to 6 bits and represented by letters and numbers (plus a couple of symbols), hence the overall size increases. This is explained very well at https://es.wikipedia.org/wiki/Base64#Examplesince 4 is 133% of 3 it is generally said that it increases the input data by 30%.
base64
it is more than anything used inSMTP
(which has a space of 7bits) that is to say email servers and relays, or to pass it by url (minus the padding=
that is going to be transformed to%3D
and must be decoded separately or calculated) a common example ofbase64
in url is to insert image files (binary) into HTML, XML or CSS (text only).In simple API's it may make sense for example to url a json encoded in
base64
and thus the URL contains all the info to generate the response (even so there are certain practical limits regarding the total length of the URL).multipart
on the contrary, it allows to send directly the data in binary (bytes of 8 bits) with theContent-Transfer-Encoding: binary
Being an API about
http
and you have to choose between both,multipart/form-data
it's the one to go.In case of uploading large files with metadata, for example in the YouTube API, what they provide is an additional method that will separate the process into two parts: you do a POST with the metadata and you receive (if everything is ok) one
200
more thelocation
one with the URI where to do the PUT of the file with thecontent-length
and thecontent-type
, this URI includes the id assigned to the file so you can resume the upload in case of unstable network, check if it is still in process or if it has already been uploaded etc..