# Multipart form data and JSON

**URL:** <https://discuss.jsonapi.org/t/multipart-form-data-and-json/2840>\
**Category:** Uncategorized\
**Created:** [May 11, 2024, 1:19am UTC](https://discuss.jsonapi.org/t/multipart-form-data-and-json/2840 "2024-05-11T01:19:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothy.stone](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/timothy.stone/32/750_2.png) [@timothy.stone](https://discuss.jsonapi.org/u/timothy.stone)\
**Post date:** [May 11, 2024, 1:19am UTC](https://discuss.jsonapi.org/t/multipart-form-data-and-json/2840/1 "2024-05-11T01:19:35Z")

</div>

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:

```json
[
  {
    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](https://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. 🤔

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

---

<div class="post-metadata">

**Author:** ![timothy.stone](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/timothy.stone/32/750_2.png) [@timothy.stone](https://discuss.jsonapi.org/u/timothy.stone)\
**Post date:** [May 11, 2024, 1:24am UTC](https://discuss.jsonapi.org/t/multipart-form-data-and-json/2840/2 "2024-05-11T01:24:31Z")

</div>

I just found this post, [_File Uploading with Multipart_](https://discuss.jsonapi.org/t/file-uploading-with-multipart/71) and [response](https://discuss.jsonapi.org/t/file-uploading-with-multipart/71/7) that almost reflects my own questions and touches on my dilemma.

---

<div class="post-metadata">

**Author:** ![sabrinaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/sabrinaa/32/882_2.png) [@sabrinaa](https://discuss.jsonapi.org/u/sabrinaa)\
**Post date:** [July 16, 2024, 7:47am UTC](https://discuss.jsonapi.org/t/multipart-form-data-and-json/2840/3 "2024-07-16T07:47:26Z")

</div>

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.
