Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best approach for picking a resource to download a file with a RestApi?

I need to download a file, but, I am wondering which is the best approach to publish my resource. Lets say I have a Document

http://api/documents/id

where I make a GET request should I receive the information of the document with that id including an extra field with the string representation of the file in BASE64? or should I publish another url resource like

http://api/documents/id/download

just for getting the file? The first I know how to do it, but I don't know if that is the proper way. With the later I need advice.

like image 796
Heinrich Avatar asked Oct 21 '25 21:10

Heinrich


1 Answers

On my understanding your document resource consists of both metadata and the actual content of the document. So you could support the following:

  • GET /documents/:id: Return a representation of the metadata of the document
  • GET /documents/:id/content: Return a representation of the content of the document

Alternatively you could support a single endpoint such as GET /documents/:id and return both metadata and content in a multipart response.


Content negotiation would be the best approach though. You could use a single endpoint such as GET /documents/:id and Accept: application/json for the metadata and, for example, Accept: application/octet-stream for the content.

like image 111
cassiomolin Avatar answered Oct 24 '25 17:10

cassiomolin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!