1

I am developing a RESTful HTTP API based on ASP.NET Core 2 and the service will be used by a web frontend.

In the API there is a resource representing a file. It comes with some meta-data (id, name, folder) and can optionally contain raw binary data representing the file contents. The rest of the API uses JSON.

What is the modern approach to representing this resource in both requests and responses? The options I found so far are:

JSON with base64-encoded data

Pros: Easy to implement and use

Cons: The binary data can be up to multiple GB in size and Base64 would therefor massively inflate traffic as well asupload and download times, additonally the data would have to be encoded and decoded which would make the process rather slow

Split the resource into a JSON and binary subresource

Pros: Easy to implement and use

Cons: Two requests must be made to access all information

BSON

Pros: No Base64

Cons: Not as easy to handle on both server and client side as JSON, data is binary even when only non-binary data is sent, has to be encoded and decoded, requires additional dependencies, format would be different from the rest of the API

multipart/form-data with JSON and binary data

Pros: Binary and non-binary data can be split into JSON and binary data

Cons: Hard to handle, format would be different from the rest of the API

Philipp
  • 876
  • 10
  • 23
  • Possible duplicate of [best approach to design a rest web service with binary data to be consumed from the browser](https://stackoverflow.com/questions/14415398/best-approach-to-design-a-rest-web-service-with-binary-data-to-be-consumed-from) – Salem Nov 12 '17 at 08:33
  • @1blustone that question is four years old does not cover BSON or new best practices – Philipp Nov 12 '17 at 09:55
  • [This answer](https://stackoverflow.com/a/37590461/7366707) is one year old and provides a quite complete explanation of different methods. – Salem Nov 12 '17 at 12:22
  • In my opinion, the most "RESTful" approach is returning JSON with a reference to where the binary can be pulled. It may require two requests, but it also makes the binary transfer optional. Returning binary directly requires more knowledge on the part of the client and closer entanglement with the web application. – Chris Pratt Nov 13 '17 at 14:36

0 Answers0