Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design a REST API to allow returning files with metadata

Tags:

rest

Suppose I'm designing a REST API and I need the clients to be able to obtain files with metadata. What is a good way to design the resources / operations?

Some ideas come to mind:

  • A single resource (i.e. GET /files/{fileId}), which returns a multi-part response containing both the file and a JSON/XML structure with metadata. I have a feeling that this is not a very good approach. For example, you cannot use the Accept header for the clients to determine if they want a XML or a JSON metadata representation, since the response type would be multi-part in both cases.

  • Two resources (i.e. GET /files/{fileId} and GET /files/{fileId}/metadata), where the first one returns the file itself and the second one a JSON/XML structure with metadata. There can be a link from the metadata to the file. However, how do I send a link to the metadata along with the file?

like image 472
Dušan Rychnovský Avatar asked Aug 01 '14 14:08

Dušan Rychnovský


People also ask

How do I get metadata from API?

You can use the GET operation to retrieve field metadata for specified fields on a specified form as defined in the criteria. The following table lists details about this GET operation. Indicates the list of field ids for which details are required. The API returns the requested data if you provide the criteria.

Can API return files?

In ASP.NET Core, a Web API action method usually returns an ActionResult object. When we want to return a file response, we can explicitly set the return type for the action method to be FileResult , which is a type inherited from ActionResult .

How do I retrieve a file from REST API?

Enter the URL of the REST Service (f.e. http://localhost:8080/rest-file-manager/resr/file/upload ) Select POST as method. Select form-data in the Body. Enter as key “attachment” of Type File.


1 Answers

I would suggest using the second idea you presented. This is the strategy used by most of the major web drives (Box, Dropbox, Google Drive, etc). They often have a significantly different URL because they store content and metadata in disparate locations.

You can add a Link header to the file response with a link to the metadata. Link headers are described in RFC 5988. The set of currently-registered link relations is here. Off the cuff, it seems that the describedBy relation is appropriate here.

like image 189
Eric Stein Avatar answered Oct 04 '22 15:10

Eric Stein