How to download files attached to a document
Today we have a question from Christian who asks how to read document files using the REST API. This is the perfect question considering that last week I answered how to actually upload them. Antoine started by answering something very practical: use the Nuxeo download URL. Which I will now translate for you.
Urls have to be in the following form:
http://<server>:<port>/nuxeo/nxbigfile/default/<doc_id>/files:files/<file_index>/file/<file_name>
Which would, for instance, give something like
http://my.server.com:8080/nuxeo/nxbigfile/default/b54c8b41-86c9-4c9b-bfe0-e6b1ca01313f/files:files/1/file/NUXEO_User%20stories.pdf
Note that here we use nxbigfile instead of nxfile (classically used from the webapp) because it’s more efficient on big files download. Beware, the index of the first file is 1, second is 2, etc…
But this is not the only solution available. As usual there are many.
You can still use the REST API and the operation adapter. First you need to get the Document URL. It could be something like http://localhost:8080/nuxeo/api/v1/id/5c911f4d-4627-4587-9ead-9b0cd1bc3dbc/
. When you have a resource, you can use adapters to do something with this resource. Here we can use the @op adapter followed by the id of the operation to use. The URL will then look like http://localhost:8080/nuxeo/api/v1/id/5c911f4d-4627-4587-9ead-9b0cd1bc3dbc/@op/Blob.Get
.
Beware, to have an appropriate server answer, you need to use HTTP POST instead of GET, because you’re calling an operation and it requires some parameters. Well in our case there is a default parameter (‘file:content’), but still, you need to send an empty params map. Using the curl command line tool, it would look like this:
curl -O -X POST -H "Content-Type: application/json+nxrequest" -d '{"params":{}}' -u Administrator:Administrator http://localhost:8080/nuxeo/api/v1/id/5c911f4d-4627-4587-9ead-9b0cd1bc3dbc/@op/Blob.Get
Do not forget the _-O_ parameter or else curl will display the content of the file to your console directly. There are other operations available to retrieve blobs. Here I used Blob.Get but you can also use Blob.GetList to get a list of blobs or Blob.GetAll to get all the blobs attached to the document. Blob lists are usually returned as a zip file.