DEV Community

Aurel
Aurel

Posted on • Originally published at blog.aurelmegnigbeto.dev

multipart/formdata: why is it recommended

Multipart/formdata is a way of transferring form data from a client to a server.

It was first described in 1995 with the RFC 1867 witch makes it one of the oldest specification about transferring huge data between computer. Let's see why is it the recommended approach to transfer some data in a client/server architecture.

TLDR

Here are some reasons why it is the recommended approach to implement file upload:

  • Possibility to transfer different kind of data inside the same request

  • Efficiency compared to bare text sending

  • Supported on all internet browsers

Let’s dive into each reason:

Possibility to transfer different kind of data inside the same request

The mulltipart/formdata specification allow sending of multiple types of content along the same request. It means that you can transfer simple text, JSON data, binary data in the payload of the request. Each part need to be separated by a boundary, identified by a name and a content-type.

In the following example, you can see that we are sending :

  • form-data

  • text/plain

  • and image/gif

Content-Type: multipart/form-data; boundary=AaB03x

   --AaB03x
   Content-Disposition: form-data; name="submit-name"

   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"
   Content-Type: multipart/mixed; boundary=BbC04y

   --BbC04y
   Content-Disposition: file; filename="file1.txt"
   Content-Type: text/plain

   ... contents of file1.txt ...
   --BbC04y
   Content-Disposition: file; filename="file2.gif"
   Content-Type: image/gif
   Content-Transfer-Encoding: binary

   ...contents of file2.gif...
   --BbC04y--
   --AaB03x--
Enter fullscreen mode Exit fullscreen mode

Efficiency compared to bare text sending

https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4

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.

  • application/x-www-form-urlencoded and application/json are designed to handle text data and not binary data. If you want to send binary data, you need to encode it in base64, which is inefficient regarding the output result size.

  • Browser handle content negotiation: The approach of sending file using JSON is not standard, and you may need to implement it yourself both on your client and the server before benefiting from it. Knowing that your browser and the server negotiate to handle the content transfer, your implementation.

Transferring a content to the browser by default use

Supported on all internet browsers

https://www.rfc-editor.org/rfc/rfc1867

multipart/formdata has been around for decades. It has been the norm for transferring file to a backend, and all the internet relies on it.

multipart/formdata is the way of transferring file, and form-data from a client to a server in an efficient way. They are some alternative, but they would be an overhead for implementation and may not be as benefiting for your development.

In the next post, we will dig deeper into how multipart/formdata is structured to send different kind of data to your server.

Top comments (0)