Multipart form data and JSON

I know I that I can post multipart/form data with boundaries via HTTP POST.

What I’m looking to confirm, and possibly get a review of, is posting a file as a base64 string as part of a larger JSON object. For example:

[
  {
    data: {
      type: documents,
      lid: 0,
      attributes: {
        foo: string,
        bar: integer,
        file: data:mime/type:ZmlsZWRhdGEK...
      ...
      ...
    }
  },
  { 
    data: {
      type: documents,
      lid: 1,
      ...
    }
  },
  { 
    data: {
      type: documents,
      lid: 2,
      ...
    }
  }
]

The file here could be a video, an excel document, an image, anything in a given set of mime types.

My initial approach is to “pair” an application/vnd.api+json (jsonapi.org) array of resources and loop through it and individual files also posted, matching the file name (or lid) to populate the associated metadata in the repository for each file in a “nested for loop.” So for n (number of files) a + 1 (json-body) document in the POST multipart/form data.

The files in question could be initially 80MB, limited by the email platform, and we could see in the future files in the GBs. Truthfully, maybe encoding a 10GB video file is highly inefficient with a large memory overhead (nevermind compressing, e.g., gzipped, on the wire by default).

Another option considered is just sending every file singly with a json-api body, so each post represents the file and metadata. More requests, but simpler handling. In other words a form accepting n documents with meta-data details would iterate n times for each file and meta-data to the API endpoint. :thinking:

Would love feedback if you’ve ever faced this problem before and have ideas.

I just found this post, File Uploading with Multipart and response that almost reflects my own questions and touches on my dilemma.

Sure! Using base64 to include files in JSON works for small files, but it’s not great for big ones due to memory and efficiency issues. For larger files like videos or big documents, using multipart/form-data is better because it handles large data more effectively and is standard for file uploads in APIs. If simplicity is more important, sending each file separately with its details might be easier, even though it means more requests. It depends on what sizes of files you’re dealing with and how your network handles them.

1 Like