multipart/form-data vs application/octet-stream
Asked Answered
C

2

74

I'm creating a simple REST API for uploading files. From other API's I found they use "multipart/form-data" content type. But for me, it looks like "application/octet-stream" is much simpler.

If I don't intend to send any more form data with the file is there any reason to use "multipart/form-data" and not "application/octet-stream" ?

Castroprauxel answered 30/3, 2015 at 13:16 Comment(1)
I have same question. Please helpPurveyance
G
42

While you don't intend to send any other data together with the file right now, multipart/form-data would give you the possibility to add additional data later on if this is required (without breaking compatability).

Also multipart/form-data would make it possible to access the REST API directly by submitting an HTML form (see https://mcmap.net/q/45052/-what-does-enctype-39-multipart-form-data-39-mean).

Your API could however support both types by using the client's content type header to distinguish between them.

Ghostly answered 9/10, 2015 at 8:32 Comment(0)
P
1

I will go with "application/form-data" for 3 main reasons:

  1. It can carry a set of different data types, including the subtype "application/octet-stream".
  2. Requirements may change in the future where you may need to post more information related to the file.
  3. Even if case 2) won't happen, you still have to consider the case where your files are large: you will need to chunk them for an effective file streaming. During the streaming process, at some point, you will need to inform your server if you sent the last file chunk or not yet (in a form of a boolean flag, for example: 'isLastChunk', chunksArray.length === 1). You will also need to tell the server which offset/chunk you are on. So this means you will post more than one data type.
Popish answered 21/12, 2023 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.