Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

backbone.js - best approach to design a rest web service with binary data to be consumed from the browser

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My research results:

  1. Single request (data included)

    The request contains metadata. The data is a property of metadata and encoded (for example: Base64).

    Pros: - transactional - everytime valid (no missing metadata or data)

    Cons: - encoding makes the request very large

    Examples: - [Twitter](https://dev.twitter.com/rest/reference/post/account/update_profile_image) - [GitHub](https://developer.github.com/v3/git/blobs/#create-a-blob) - [Imgur](http://api.imgur.com/endpoints/image)

  2. Single request (multipart)

    The request contains one or more parts with metadata and data.

    Content types:

    Pros:

    • transactional
    • everytime valid (no missing metadata or data)

    Cons: - content type negotiation is complex - content type for data is not visible in [WADL](https://www.w3.org/Submission/wadl/)

    Examples:

    • Confluence (with parts for data and for metadata)
    • Jira (with one part for data, metadata only part headers for file name and mime type)
    • Bitbucket (with one part for data, no metadata)
    • Google Drive (with one part for metadata and one for part data)
  3. 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: - transactional - everytime valid (no missing metadata or data)

    Cons: - no nested metadata possible

    Examples: - S3 [GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) and [PutObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html)

  4. Two request

    One request for metadata and one or more requests for data.

    Pros:

    • scalability (for example: data request could go to repository server)
    • resumable (see for example Google Drive)

    Cons: - not transactional - not everytime valid (before second request, one part is missing)

    Examples: - [Google Drive](https://developers.google.com/drive/v2/web/manage-uploads#simple) - [YouTube](https://developers.google.com/youtube/v3/docs/videos/insert)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...