I'm developing a json rest web service that will be consumed from a single web page app built with backbone.js
This API will let the consumer upload files related to some entity, like pdf reports related to a project
Googling around and doing some research at stack overflow I came with these possible approaches:
First approach: base64 encoded data field
POST: /api/projects/234/reports { author: 'xxxx', abstract: 'xxxx', filename: 'xxxx', filesize: 222, content: '<base64 encoded binary data>' }
Second approach: multipart form post:
POST: /api/projects/234/reports { author: 'xxxx', abstract: 'xxxx', }
as a response I'll get a report id, and with that I shall issue another post
POST: /api/projects/234/reports/1/content enctype=multipart/form-data
and then just send the binary data
(have a look at this: https://stackoverflow.com/a/3938816/47633)
Third approach: post the binary data to a separate resource and save the href
first I generate a random key at the client and post the binary content there
POST: /api/files/E4304205-29B7-48EE-A359-74250E19EFC4 enctype=multipart/form-data
and then
POST: /api/projects/234/reports { author: 'xxxx', abstract: 'xxxx', filename: 'xxxx', filesize: 222, href: '/api/files/E4304205-29B7-48EE-A359-74250E19EFC4' }
(see this: https://stackoverflow.com/a/4032079/47633)
I just wanted to know if there's any other approach I could use, the pros/cons of each, and if there's any established way to deal with this kind of requirements
the big con I see to the first approach, is that I have to fully load and base64 encode the file on the client
some useful resources:
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.
When a GET method returns binary data, the Swagger document that is generated by IBM® Cúram Social Program Management specifies the Response Content type as anything other than application/json. This response indicates that binary content and not JSON content is provided in the response.
One of the key advantages of REST APIs is that they provide a great deal of flexibility. Data is not tied to resources or methods, so REST can handle multiple types of calls, return different data formats and even change structurally with the correct implementation of hypermedia.
My research results:
Single request (data included)
The request contains metadata. The data is a property of metadata and encoded (for example: Base64).
Pros:
Cons:
Examples:
Single request (multipart)
The request contains one or more parts with metadata and data.
Content types:
Pros:
Cons:
Examples:
Single request (metadata in HTTP header and URL)
The request body contains the data and the HTTP header and the URL contains the metadata.
Pros:
Cons:
Examples:
Two request
One request for metadata and one or more requests for data.
Pros:
Cons:
Examples:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With