Jake Miller Jake Miller - 1 year ago 59
HTTP Question

How to retrieve metadata associated with a video or image sent from a RESTful web service?

I have the controller shown below:

@RequestMapping(value = "/videos/{id}",
headers = "Accept=image/jpeg, image/jpg, image/png, video/mp4",
method = RequestMethod.GET)
public ResponseEntity<byte[]> loadVideo(@PathVariable("id") long campaignId,
Principal principal) throws IOException {

This controller returns a byte stream of the media associated with the given id. It works fine. The only issue I'm having is loading this videos associated metadata (title, description, view count, etc...) as I'm sending back an array of bytes, so I'm not too sure where to put the meta data.

  • Should I place the metadata in the response headers?

  • Should I have two separate calls, one for the video (byte steam) and
    another call which returns an object containing the meta data?

  • Or is there a better way to go about this than either of the two
    options above?

Answer Source

As my comment was already lenghty I decided to repost it here again:

If you deal with certain media-types like image/jpg or video/mp4 you should include the metadata as headers as the payload of the response should only include the bytes of the respective file. This also enables a lookup of the metadata without having to download the bytes of the actual file via a simple HEAD request.

Certain API provides, howerver, define their own media-type or send a JSON or XML based response to the client. In this cases, the payload contains often a predefined structure which includes the bytes of the file as a base64 encoded string as well as the metadata as plain text. These APIs argument that sending multiple files at once is easier this way then to handle multipart content.