REST API download attachment

I know this question has been asked before and was even a subject of a blog article, but answers to those were focused on curl like URLs and not so much on input/output variables. So:

With Blob.Get one can get either the main-document or a document attached to a main-document depending on the parameters being passed. General layout would be (in PHP, but more or less generic):

->newRequest(“Blob.Get”) ->set('input', 'doc:'' . $idoc) ->set('input', 'docs:” . $idocs) ->set('param', 'xpath', $xpath)

docs and xpath are optional

Depending on what you provide one can get returned:

  • error 666 (not documented, but can be anything as it is the only error(number) which is implemented)
  • some weird zip-file with the name+extension of the main document containing a structure of xml files
  • the main document

So far I failed to get an attachment using the rest API. As the 'data' property contains the full URL it can be done using curl or the likes. But how to commence using the REST API itself (call returning a blob as can be expected of blob.get)? docs:utl from data? xpath:files:content[no-of-attachment] or … ?

Is this documented with examples somewhere?

0 votes

1 answers

11668 views

ANSWER

09/14/2016

Probably the closest thing to documentation on the subject is: https://doc.nuxeo.com/display/NXDOC/URLs+for+files However that one also does not address what the content of the parameters should be. For instance:

  • main document path: /default-domain/workspaces/Schmidt/Ik ben lekker stout.odt
  • desired attachment to be fetched: Spin Sebastiaan.jpg
  • number of attachments: 3 (0-2). This one being the first (0)
  • full url:

probably: ->set('param', 'xpath', 'files:files/1/file') But what about ->set('doc:'', '') ? Or must this be an ecm:path or …. ? Regardless what I add there I always seem to end up with error 666

09/20/2016



Hi olaf, As explained in the Nuxeo Automation Commands doc, the Blob.Get operation need documents as input and accept an XPath to a property. The documents input are explained in the Nuxeo Command Endpoint doc:

doc:/default-domain/workspaces/myworkspace” or “doc:96bfb9cb-a13d-48a2-9bbd-9341fcf24801

docs:/default-domain/workspaces/myworkspace, 96bfb9cb-a13d-48a2-9bbd-9341fcf24801

Although your XPath is correct with files:files/1/file, what you can find with the JSON/XML export of your main document.

Note that the current version of the PHP Automation Client creates as tempstream file when using the Blob.Get operation (a rewrite of the client is under way), you can find an example in the sample B5 in the sources of the client.

With the previous data, you should be able to make a running code as the following :

function GetBlob($path = '/default-domain/workspaces/Default Workspace/Some note') {
    $client = new \Nuxeo\Automation\Client\NuxeoPhpAutomationClient('http://nuxeo:8080/nuxeo/site/automation');
    $session = $client->getSession('Administrator', 'Administrator');
    $answer = $session->newRequest("Blob.Get")->set('input', 'doc:' . $path)->set('params', 'xpath', 'files:files/0/file')->sendRequest();
    if (!isset($answer) OR $answer == false)
        echo '$answer is not set';
    else {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=image.png');
        readfile('tempstream');
    }
}
0 votes



Thank you Pierre-Gildas Millon. When creating this new client could you please consider using http://php.net/manual/en/function.tempnam.php instead of http://php.net/manual/en/function.file-put-contents.php for file streaming? The current implementation requires extended rights which can not be adjusted without recoding/adapting the Nuxeo PHP client. Also: the current PHP client is listed on the Nuxeo REST API page is a not fully implemented client. Could you specify in that document if the new client will be a fully implemented library, and if not, what is missing?
09/30/2016

The present version is an Automation API client, is does not implement the full Rest API of Nuxeo. The next release is a major rewrite of the client but won't offer new features, it will stay an Automation API client. Further releases will implement new features from the Nuxeo Rest API.
10/17/2016