File upload API: Multipart / form-data and the original content in the body?

I noticed that there are (at least) two ways to upload a file to an HTTP server through the API.

You can use multipart/form-data (this is what browsers do initially to load HTML input files), but you can also POST contents of the file inside the request body (possibly with the correct Content-Type request header).

What are the pros and cons of each method (in general, and not from the browser)?

Multi-page requests, for example, depending on which http or network library you use in your programming environment (I use Node.js on the server side and Swift on the client side) - it seems a little harder to create and then parse.

+5
source share
1 answer

The only difference at the protocol level is that multipart/form-data requests must adhere to RFC 2388 , while a custom request body type can be arbitrary.

The practical significance of this is that the multipart/form-data request is usually larger: while clients are technically allowed to use non-7bit content-transfer-encoding , base64 is used by most. MIME headers generate additional overhead, which can become a bottleneck if many small files are uploaded. Please note that support for downloading multipart/form-data files in existing clients / libraries is much wider. You should always provide it as a backup if you are not sure enough that you have clients and intermediate hosts (proxies). Especially keep in mind that if you are developing an API for third-party developers that other developers will already be familiar with multipart/form-data and have libraries at hand to work with this.

+8
source

All Articles