Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using multipart/form-data any better then JSON + Base64?

Tags:

I have a server and I need to upload files along with some fields from the client to the server. I have currently been using standard multipart/form-data.

I have found however that using multipart/form-data is not ideal. Objects on my server may have other objects nested within them, and thus are represented as a JSON object with other JSON objects embedded within.

I would like for the client to start making POST/PUT requests using a JSON representation exactly like it would expect in a GET request to the server, in a REST-ful manner. This way I don't have to flatten the fields which might be nested a couple layers within the JSON object in order to use multipart/form-data.

Problem is, JSON doesn't represent binary data. Multipart/form-data doesn't seem to have a way to represent fields nested within the values of other fields. But it does have much better handling of file-uploads.

I am at a loss for how to design this. Should I just have the client upload JSON with the fields encoded in base64, and take the 25% hit? Or should I have the JSON object being represented as some sort of "json" variable in a Multipart/form-data request, and have the binary files to be uploaded as another variable?

like image 423
genixpro Avatar asked Aug 14 '13 19:08

genixpro


People also ask

Does multipart form data use Base64?

cases, binary multipart/form-data data is encoded as base64.

What is the difference between Base64 and multipart?

Base64-encoded data is approximately 33% larger, meaning longer upload times, more bandwidth used, and a lower maximum file size for non-multipart uploads. You can insert or update blob data using a non-multipart message, but you are limited to 50 MB of text data or 37.5 MB of base64–encoded data.

When would you use a multipart?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).

Is multipart form data restful?

Multipart/Form-Data is a popular format for REST APIs, since it can represent each key-value pair as a “part” with its own content type and disposition. Each part is separated by a specific boundary string, and we don't explicitly need Percent Encoding for their values.


1 Answers

Should I just have the client upload JSON with the fields encoded in base64, and take the 25% hit?

The hit will be 33% since 4/3=1.33.

Or should I have the JSON object being represented as some sort of "json" variable in a Multipart/form-data request, and have the binary files to be uploaded as another variable?

This should work.

You might also consider this approach: send all files using multipart, then get some identificators of files as a response. Put this identificators in your json and send it anyway you like. This approach might be beneficial if you have many scenarios in which you send files: you might always send them to the server with the same request, then get their identificators; after that do with them what you like.

like image 142
Anton Ryabyh Avatar answered Nov 09 '22 11:11

Anton Ryabyh