2

I am writing project which is simple web file repository. A client sends me a file with POST:

POST /file
<binary data>

and gets hash of the file as a response.

He can send images, documents, etc. I would like to provide him a best way to retrieve those files from the application with hashes. So he could call:

GET /file/{hash}

And retrieve binary version of file.

Or maybe the better approach is to give him the file properties like:

{
    type: "image/png"
    bytes: 1024,
    properties: [{
        width: 100,
        height: 200
    }]
}

And then under URLs:

GET /file/{hash}?base64

He will get Base64 encoded file, and under

GET /file/{hash}?binary

will be binary version of file responded? Which approach is better and more RESTful?

Opal
  • 81,889
  • 28
  • 189
  • 210
Dariusz Mydlarz
  • 2,940
  • 6
  • 31
  • 59
  • 1
    http://stackoverflow.com/questions/12239868/whats-the-correct-way-to-send-a-file-from-rest-web-service-to-client this might help – seenukarthi Aug 11 '15 at 08:00

2 Answers2

2

I think it's up to your needs. I will go with the former if you don't need the extra file properties.

By the way, maybe you will find inspiring to take a look to Google Drive API: https://developers.google.com/drive/v2/reference/

Robles
  • 123
  • 1
  • 10
2

Theoretically when you send response to POST method you should return an ID, along with the body you received and 201 Created HTTP status code. Of course it doesn't make sense at all in this particular case. So:

  1. The response to POST request should be marked with 301 Moved Permanently and have Location (e.g. /files/{hash} header set which points to an endpoint where client can get files data.

  2. If client requests to URL from mentioned Location header it may receive a JSON content with basic file description (size, content type, hash, whatever is needed)

  3. A separate endpoint (e.g. /files/{hash}/data/) should be provided to return files raw data. Client also should have a possibility to set a data format it would like to get but not via query parameter name only (?binary, ?base64) but with key-value pair. (?format=binary).

Opal
  • 81,889
  • 28
  • 189
  • 210