3 votes

Is it a best practice to use multipart/form-data instead of base64 when receiving files in an API?

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 two conditions:

  • Upload a file for each request using multipart/form-data i.e. the file travels directly embedded in the request.
  • Upload a file for each request by sending it in the body as a string encoded in base64

Both actions work and receive the file (it is manipulated depending on the format in which the API receives it).

Ask

Is there an advantage or a best practice in using multipart/form-data instead of base64 under the above scenario?

1 votes

base64 is a way of coding that enlarges the data by up to 30%, multipart/form-data is part of the standard http and allows to send binary data, because of that multipart is faster and consumes less bandwidth.

1 votes

base64 transforms binary data into a "printable" ASCII representation, the 8-bit space is reduced to 6 bits and represented with letters and numbers (plus a couple of symbols), hence the overall size increases. en.wikipedia.org/wiki/Base64

0 votes

multipart, on the other hand, allows you to send data directly in binary (8-bit bytes) with the Content-Transfer-Encoding: binary w3.org/TR/html401/interact/forms.html#h-17.13.4.2

2voto

Einer Points 16588

base64 is the conversion of bytes to characters ASCII . It is not recommended that you use base64 as it adds more weight to the request. For example the simple character hola which are 4 bytes converted to base64 do 8 bytes (hello in base64 is aG9sYQ== ) 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 keep in mind is that base64 has to be decoded on the server adding more processing. If you send a +10MB file you would have to do that first:

  1. Convert to base64 on the device: something that is very slow in itself.
  2. Sending the data to the server: slow and cumbersome due to the amount of data to be converted
  3. Decoding them on the server: slow and unnecessary process.

In the case of multipart, it 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.

Advantages:

  1. Support on all HTTP servers
  2. Does not add to the computational burden of file uploads.
  3. Any file can be sent.

2voto

alo Malbarez Points 6463

base64 is a way of coding that enlarges the data by up to 30%, multipart/form-data is part of the standard http and allows to send binary data, because of that multipart is faster and consumes less bandwidth

base64 transforms binary data to a "printable" ASCII representation, the 8-bit space is reduced to 6 bits and represented with letters and numbers (plus a couple of symbols), hence the overall size increases. This is explained very well in https://es.wikipedia.org/wiki/Base64#Ejemplo

for this, it uses a 24-bit buffer: it puts in 3 bytes of 8 bits ( 8 x 3 = 24 ) and outputs 4 "letters" of 6 bits each ( 6 x 4 = 24 ).

as 4 is 133% of 3 is generally said to increase the input data by 30%.

base64 is mostly used in SMTP (which has a space of 7bits) i.e. email servers and relays, or to pass it by url (minus the padding = which is to be converted to %3D and has to be decoded separately or calculated) a common example of a base64 in url is to insert image files (binary) in HTML, XML or CSS (text only).

In simple API's it can make sense for example to pass by url a json encoded in base64 and so the URL contains all the info to generate the response (there are still some practical limits to the total length of the URL).

multipart on the other hand, it allows data to be sent directly in binary (8-bit bytes) with the Content-Transfer-Encoding: binary

Being an API on http and you have to choose between the two, multipart/form-data is the one that goes.

In the 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 in two parts: you make a POST with the metadata and you receive (in case everything is ok) a 200 but the location with the URI to where to PUT the file with the content-length and the content-type This URI includes the id assigned to the file so you can summarize the upload in case of unstable network, check if it is still in process or if it is already uploaded etc

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X