Changes

Jump to navigation Jump to search
813 bytes added ,  15:30, 10 April 2019
no edit summary
* {{Code|query}}: Evaluate an XQuery expression. If a database or database path is specified in the URL, it is used as initial query context.
* {{Code|command}}: Execute a single [[Commands|database command]].
* {{Code|run}}: Evaluate an XQuery file or command script located on the server. The file path is resolved against the directory specified by <code>[[Options#WEBPATH{{Option|RESTPATH]]</code> }} (before, it was resolved against <code>[[Options#WEBPATH{{Option|WEBPATH]]</code>}}).
; Examples
==POST Method==
The body of a POST request is interpreted as XML fragment, which specifies theoperation to perform. The name of the root element determines how the body must conform to a given will be evaluated: * {{Code|commands}}: Run [[REST: POST SchemaCommands#Command Scripts|XML SchemaCommand Script]]* {{Code|query}}: Execute XQuery expression* {{Code|run}}: Run server-side file (query or command script)* {{Code|command}}: Execute single command  The root element may be bound to the optional REST namespace.Existing command scripts can be sent to the server without any modifications: * Create an empty database and return database information: <pre class="brush:xml"><commands> <create-db name='db'/> <info-db/></commands></pre> For the other commands, the following child elements are supported: {| class="wikitable"|-! Name! Description|-| {{Code|text}}| Required; contains the query string, command string, or file to be run|-| {{Code|parameter}}| Serialization parameter (with {{Code|@name}} and {{Code|@value}} attributes)|-| {{Code|option}}| Database option (with {{Code|@name}} and {{Code|@value}} attributes)|-| {{Code|variable}}| Variable bindings|-| {{Code|context}}| Initial context item|}
; Examples
* The following query returns Return the first five city names of the <b>factbook</b> database:
<pre class="brush:xml">
<rest:query xmlns:rest="http://basex.org/rest"> <rest:text><![CDATA[ (//city/name)[position() <= 5] ]]></rest:text></rest:query>
</pre>
* The second query returns the Return string lengths of all text nodes, which that are found in the node that has been specified as initial context node:
<pre class="brush:xml">
<rest:query xmlns:rest="http://basex.org/rest"> <rest:text>for $i in .//text() return string-length($i)</rest:text> <rest:context>
<xml>
<text>Hello</text>
<text>World</text>
</xml>
</rest:context></rest:query>
</pre>
* The following request returns Return the registered database users encoded in <code>ISO-8859-1</code>:
<pre class="brush:xml">
<command xmlns="http://basex.org/rest">
<text>show users</text>
<parameter name='encoding' value='ISO-8859-1'/>
</pre>
* This example creates Create a new database from the specified input and retains preserve all whitespaces:
<pre class="brush:xml">
<command xmlns="http://basex.org/rest">
<text>create db test http://files.basex.org/xml/xmark.xml</text>
<option name='chop' value='false'/>
</pre>
* The last request runs a Bind value to the {{Code|$person}} variable and run query <code>queryfind-person.xq</code> , which must be located in the directory specified by <code>[[Options#WEBPATH{{Option|WEBPATH]]</code>}}:
<pre class="brush:xml">
<run xmlns> <variable name="http://basex.org'person' value='Johannes Müller'/rest"> <text>queryfind-person.xq</text>
</run>
</pre>
There are two ways to store non-XML data in BaseX:
* '''Store as rawRaw Data''':<br/> If <code>application/octet-stream</code> is chosen as content-type, the input data is added as raw[[Binary Data]].
* '''Convert to XML''':<br/> Incoming data is converted to XML if a parser is available for the specified content-type. The following content types are supported:
** <code>application/json</code>: Stores JSON as XML.
** <code>text/html</code>: Stores HTML input as XML.
Since {{Version|8.1}}, conversion Conversion can be influenced by specifying additional content-type parameters (see [[RESTXQ#Content Types|RESTXQ]] for more information).
If raw data is added and if no content type, or a wrong content, is specified, a <code>400</code> (BAD REQUEST) error will be raised.
# If the serialization parameter <code>[[Serialization|media-type]]</code> is supplied, it will be adopted as content-type.
# Otherwise, if the serialization parameter <code>[[Serialization|method]]</code> is supplied, the content-type will be chosen according to the following mapping:
#* <code>xml</code>, <code>adaptive</code>, <code>basex</code> → <code>application/xml</code>
#* <code>xhtml</code> → <code>text/html</code>
#* <code>html</code> → <code>text/html</code>
#* <code>text</code> → <code>text/plain</code>
#* <code>json</code> → <code>application/json</code>
#* <code>raw</code> → <code>application/octet-stream</code> (binary data will be sent in its original byte representation, i. e., without further conversion)
# If no media-type or serialization method is supplied, the content type of a response depends on the chosen REST operation:
#* '''Query'''/'''Run''' → <code>application/xml</code>
=Changelog=
 
;Version 9.0
* Added: Support for command scripts in the [[#POST Method|POST Method]].
* Updated: The REST namespace in the [[#POST Method|POST Method]] has become optional.
;Version 8.1
* Added: support Support for input-specific content-type parameters* Updated: the The [[#GET RequestsMethod|run operation]] now resolves file paths against the [[Options#RESTPATH{{Option|RESTPATH]] }} option.
;Version 8.0
;Version 7.9
* Updated: Also evaluate command scripts via the <code>[[#GET RequestsMethod|run]]</code> operation.
;Version 7.2
* Removed: direct Direct evaluation of adresses resources with <code>application/xquery</code> as content type
;Version 7.1.1
;Version 7.0
* REST API introduced, replacing the old JAX-RX API
 
[[Category:Server]]
[[Category:HTTP]]
[[Category:Developer]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu