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

前端 未结 3 1542
礼貌的吻别
礼貌的吻别 2020-12-12 18:25

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 t

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 18:49

    I believe the ultimate method is number 3 (separate resource) for the main reason that it allows maximizing the value I get from the HTTP standard, which matches how I think of REST APIs. For example, and assuming a well-grounded HTTP client is in the use, you get the following benefits:

    • Content compression: You optimize by allowing servers to respond with compressed result if clients indicate they support, your API is unchanged, existing clients continue to work, future clients can make use of it
    • Caching: If-Modified-Since, ETag, etc. Clients can advoid refetching the binary data altogether
    • Content type abstraction: For example, you require an uploaded image, it can be of types image/jpeg or image/png. The HTTP headers Accept and Content-type give us some elegant semantics for negotiating this between clients and servers without having to hardcode it all as part of our schema and/or API

    On the other hand, I believe it's fair to conclude that this method is not the simplest if the binary data in question is not optional. In which case the Cons listed in Eric Hu's answer will come into play.

提交回复
热议问题