Import Binary Data as multipart or base64 inline #55
Replies: 1 comment 1 reply
-
Multipart formdata is very attractive, but not well supported by clients. Often special extensions are needed which aren't very widely used or in some cases the format has to be handled manually. One consideration is payload size. This can make encodings like base64 unmanageable. A single binary format can make it easier to support range requests for capable clients to resume interrupted downloads. This can also make it much easier for browsers to open the content directly when that is desired. One thought that is under consideration for parcel service is whether and how to support resuming uploads. One option is to support uploading in chunks. This is supported by s3. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It is difficult to mix binary data with RESTful resources. For example, when you want a RESTful endpoint that creates a new object, but that item is a combination of metadata and binary data, then where do the binary data and metadata get included?
Option 1: Headers & Content-Type (octet-stream)
In this scenario, we use the "Content-Type" as intended to tell the request that the entire incoming body is of type
application/octet-stream
for incoming binary data like an image, or xls spreadsheet. The metadata about that object is included in headers.Generally speaking, this is not ideal headers are a bit more unfriendly and limited in their flexibility/discoverability.
Option 2: Multipart/FormData
This is more flexible than the first option enabling additional form data to be added as part of the payload/form-data directly.
Option 3: Body + Base64 Binary Inline
In this scenario, we make our life simple by handling standard JSON in our restful resource which is very consistent. The binary data is included in a base64 string value representing the spreadsheet that must be converted (both by client and backend).
Some implications: https://stackoverflow.com/questions/65350640/to-upload-a-file-what-are-the-pros-and-cons-of-sending-base64-in-post-body-vs-mu
Option 4: Separate Metadata and Binary Endpoints
In this scenario, it is a hybrid between the above approaches. There is too much metadata to be specified as all headers or can even be confusing as purely form-data. Additionally, using
multipart/form-data
for partial upload is not an option with base64 encoding. The overhead in compute and size of base64 encoding makes option 3 very specific for certain circumstances.Create a spreadsheet object that we can then upload other types of content too:
This endpoint provides back a unique resource id we can replace for the content
This hybrid pattern while two calls, does enable the best of both worlds:
Some comparisons:
In simpler scenarios combining JSON and multipart endpoints works well:
Using RESTful resources with either
application/json
ormultipart/form-data
as originally intended and extending to sub-resources if complex enough feels like a natural approach based on the available primitives. Thoughts?Beta Was this translation helpful? Give feedback.
All reactions