Copy document via REST API
Wednesday, 26 February 2025 · 4 messages · last reply 2 Mar 2025
LA
Liedtke, Alexander26 Feb 2025 15:21 #
Hello all,
I am trying to copy (duplicate) an existing document via REST API, but can not get it working.
I tried via POST as commands. Can somebody please provide me a working example ?
My use case is simply duplicate an existing document in a databse, as a backup file.
From the documentation: <add (path='...')>[input]</add>,
I translated it to xml command similar:
<commands>
<command>OPEN db</command>
<command>ADD copy.xml source.xml</command>
</commands>with diffferent variations, like adding path argument etc.
However, in the end I got nothing working. I also tried it via xquery,
but I would prefer xml like above. But in the end I am happy with any working example.
Two more questions in that context. What is the recommended header Content-Type xml or text/plain?
Does it make sense rto use the http://basex_rest_url/?command url or just the REST URL?
I am pretty sure, there is an easy solution for copying documents, so any help on this is appreciated.
Thank you in advance
Alexander Liedtke
CG
Christian Grün27 Feb 2025 16:04 #
Hi,
Quoted text · Liedtke, Alexander · 1 line
> I tried via POST as commands. Can somebody please provide me a working
example ?
There is no dedicated command for duplicating resources in a single database, but you can use XQuery (with the available COPY command, the full database is duplicated). The following command script presents one way to do it:
<commands>
<create-db name='test'/>
<add path='source.xml'><xml/></add>
<xquery>
db:add('test', db:get('test', 'source.xml'), 'target.xml')
</xquery>
<xquery>.</xquery>
</commands>Quoted text · Liedtke, Alexander · 1 line
> Two more questions in that context. What is the recommended header
Content-Type xml or text/plain?
It may depend on the… context ;·) Feel free to give us more information on your use case.
Quoted text · Liedtke, Alexander · 1 line
> Does it make sense rto use the http://basex_rest_url/?command url or
just the REST URL?
The 'command' query parameter is sufficient for simple requests, but you might encounter limits when supplying more data. Next, it should not be used to set passwords, as GET requests will end up in the database logs (unless this is prevented by setting specific options).
Hope this helps,
Christian
LA
Liedtke, Alexander27 Feb 2025 17:16 #
Thanks a lot Christian - works like a charm!
<commands>
<xquery>db:add("db_name", db:get("db_name", "source.xml"), "copy.xml")</xquery>
</commands>was all that what I needed. (the
db:create part was not neccessary for me, because the db already exists.)
One final question and your opinion.
Do you think this is a reliable perfomant solution to copy big files (can not specifiy how big am, but could be
estimated more than 500MB ) - possible limitations in xquery ?
I am still in early development and I have the chance to change my db structure and organize that
I could also use db copy. However the possibility to duplicate single files this way for backup etc would be a nice option.
So I would really appreciate your opinion on this.
Thanks a again for your solution.
Best regards
Alexander LiedtkeQuoted text · Christian Grün · 23 lines
Von: Christian Grün <christian.gruen at gmail.com> Gesendet: Donnerstag, 27. Februar 2025 16:04:39 An: Liedtke, Alexander Cc: basex-talk at mailman.uni-konstanz.de Betreff: [EXT] Re: [basex-talk] Copy document via REST API EXTERNE E-MAIL - SEIEN SIE ACHTSAM BEIM ÖFFNEN VON LINKS UND ANHÄNGEN Hi, > I tried via POST as commands. Can somebody please provide me a working example ? There is no dedicated command for duplicating resources in a single database, but you can use XQuery (with the available COPY command, the full database is duplicated). The following command script presents one way to do it: <commands> <create-db name='test'/> <add path='source.xml'><xml/></add> <xquery> db:add('test', db:get('test', 'source.xml'), 'target.xml') </xquery> <xquery>.</xquery> </commands> > Two more questions in that context. What is the recommended header Content-Type xml or text/plain? It may depend on the… context ;·) Feel free to give us more information on your use case. > Does it make sense rto use the http://basex_rest_url/?command url or just the REST URL? The 'command' query parameter is sufficient for simple requests, but you might encounter limits when supplying more data. Next, it should not be used to set passwords, as GET requests will end up in the database logs (unless this is prevented by setting specific options). Hope this helps, Christian
CG
Christian Grün2 Mar 2025 14:52 #
Hi Alexander,
Quoted text · Liedtke, Alexander · 4 lines
> Do you think this is a reliable perfomant solution to copy big files (can > not specifiy how big am, but could be > estimated more than 500MB ) - possible limitations in xquery ? >
I would say so. Some bulk operations are only available via commands, but a lot of functionality for commands and XQuery is based on the same code. To reduce memory consumption, you can enable the
ADDCACHE option [1], but it will slow down the execution a bit.
As databases are lightweight objects in BaseX, you can also write all your files to a dedicated backup database.
Best,
Christian
[1] https://docs.basex.org/main/Database_Functions#db:addWritten for BaseX 11.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery