Changes

Jump to navigation Jump to search
303 bytes added ,  10:29, 3 August 2022
m
Text replacement - "8984" to "8080"
=Usage=
By default, REST services are available at {{Code|http://localhost:89848080/rest/}}. If no default credentials are specified in the URL or when starting the web application, they will be requested by the client ([[Web Application#User Management|see further]]).
A web browser can be used to perform simple GET-based REST requests and display the response. Some alternatives for using REST are listed in the [[#Usage Examples|Usage Examples]].
 
With {{Announce|BaseX 10}}, results in the {{Code|rest}} namespace will be returned unprefixed:
 
<syntaxhighlight lang="xml">
<!-- before -->
<rest:databases xmlns:rest="http://basex.org/rest"/>
 
<!-- before -->
<databases xmlns="http://basex.org/rest"/>
</syntaxhighlight>
=URL Architecture=
 
A request to the root URL returns all available databases:
The root URL lists all available databases. The following examples assume thatyou have created a database instance from the [https://files.basex.org/xml/factbook.xml factbook.xml] document: :<code>http://localhost:89848080/rest</code>
<syntaxhighlight lang="xml">
<rest:databases resources="1" xmlns:rest="http://basex.org/rest"> <rest:database resources="125742" size="181359943813599">factbookarticles</rest:database> ...</rest:databases>
</syntaxhighlight>
The resources of a database can be (directories, resource metadata) are listed by specifying the if a database, and potential sub directories, in the URL.In the given example, a single XML document an optional directory path is stored in the ''factbook'' databasespecified:
:<code>http://localhost:89848080/rest/factbookarticles</code>
<syntaxhighlight lang="xml">
<rest:database name="factbook" resources="1articles" xmlns:rest="http://basex.org/rest"> <rest:dir>binaries</dir> <resource type="xml" content-type="application/xml" size="77192">factbook1973-02-08-xltp325.xml</rest:resource> ...</rest:database>
</syntaxhighlight>
 
The {{Code|dir}} elements were introduced with {{Announce|Version 10}}. Before, information on all resources was listed that were located in the specified path or any of its subdirectories.
The contents of If the path to a database can be retrieved by directly addressing single resource is specified, the resourceitself will be returned:
:<code>http://localhost:89848080/rest/factbookarticles/factbook1973-02-08-xltp325.xml</code> If a resource is not found, an HTTP response will be generated with <code>404</code> as status code.
==Parameters==
; Examples
* Lists all resources found in the '''tmp''' path of the ''factbook'' database:<br/><code>http://localhost:89848080/rest/factbook/tmp</code>
* Returns the number of documents in a database:<br/><code>http://localhost:89848080/rest/database?query=count(.)</code>
* Serializes a document as JSONML:<br/><code>http://localhost:89848080/rest/factbook/factbook.xml?method=json&json=format=jsonml</code>
* <code>US-ASCII</code> is chosen as output encoding, and the query <code>eval.xq</code> is evaluated:<br/><code>http://localhost:89848080/rest?run=eval.xq&encoding=US-ASCII</code>
* The next URL lists all database users that are known to BaseX:<br/><code>http://localhost:89848080/rest?command=show+users</code>
==POST Method==
* Return the first five city names of the <b>factbook</b> database:
<syntaxhighlight lang="xml">
<rest:query xmlns:rest="http://basex.org/rest"> <rest:text><![CDATA[ (//city/name)[position() <= 5] ]]></rest:text></rest:query>
</syntaxhighlight>
; Examples
* A new database with the name <b>XMark</b> is created. If XML input is sent in the HTTP body, the resulting database resource will be called <b>XMark.xml</b>:<br/><code><nowiki>http://localhost:89848080/rest/XMark</nowiki></code>* A new database is created, and no whitespaces will be removed from the passed on XML input:<br/><code><nowiki>http://localhost:89848080/rest/XMark?chop=false</nowiki></code>* The contents of the HTTP body will be taken as input for the document <b>one.xml</b>, which will be stored in the <b>XMark</b> database:<br/><code><nowiki>http://localhost:89848080/rest/XMark/one.xml</nowiki></code>
An HTTP response with status code <code>201</code> (CREATED)
; Example
* The <b>factbook</b> database is deleted:<br/><code><nowiki>http://localhost:89848080/rest/factbook</nowiki></code>* All resources of the <b>XMark</b> database are deleted that reside in the <b>tmp</b> path:<br/><code><nowiki>http://localhost:89848080/rest/XMark/tmp/</nowiki></code>
The HTTP status code <code>404</code> is returned if no database is specified.
; Example
* The following request assigns two variables to a server-side query file <code>mult.xq</code> placed in the HTTP directory:<br/><code>http://localhost:89848080/rest?run=mult.xq&$a=21&$b=2</code>
<syntaxhighlight lang="xquery">
(: XQuery file: mult.xq :)
<query xmlns="http://basex.org/rest">
<text><![CDATA[
declare variable $x a as xs:integer external; declare variable $y b as xs:integer external;
<mult>{ $a * $b }</mult>
]]></text>
The following three example requests all return <code>&lt;a/&gt;</code> with <code>application/xml</code> as content-type:
:<code>http://localhost:89848080/rest?query=%3Ca/%3E</code><br/><code>http://localhost:89848080/rest?query=%3Ca/%3E&method=xml</code><br/><code>http://localhost:89848080/rest?query=%3Ca/%3E&media-type=application/xml</code>
=Usage Examples=
public static void main(String[] args) throws Exception {
// The java URL connection to the resource.
URL url = new URL("http://localhost:89848080/rest/factbook");
// Establish the connection to the URL.
String user = "bob";
String pw ="alice";
// Encode user name username and password pair with a base64 implementation.
String encoded = Base64.encode(user + ":" + pw);
// Basic access authentication header to connection request.
==Command Line==
Tools such as the Linux commands [httphttps://www.gnu.org/s/wget/ Wget] or [http://curl.haxx.se/ cURL] exist to
perform HTTP requests (try copy &amp; paste):
;GET
* <code><nowiki>curl -i "http://localhost:89848080/rest/factbook?query=//city/name"</nowiki></code>
;POST
* <code><nowiki>curl -i -X POST -H "Content-Type: application/xml" -d "&lt;query xmlns='http://basex.org/rest'&gt;&lt;text&gt;//city/name&lt;/text&gt;&lt;/query&gt;" "http://localhost:89848080/rest/factbook"</nowiki></code>* <code><nowiki>curl -i -X POST -H "Content-Type: application/xml" -T query.xml "http://localhost:89848080/rest/factbook"</nowiki></code>
;PUT
* <code><nowiki>curl -i -X PUT -T "etc/xml/factbook.xml" "http://localhost:89848080/rest/factbook"</nowiki></code>* <code><nowiki>curl -i -X PUT -H "Content-Type: application/json" -T "plain.json" "http://localhost:89848080/rest/plain"</nowiki></code>
;DELETE
* <code><nowiki>curl -i -X DELETE "http://admin:admin@localhost:89848080/rest/factbook"</nowiki></code>
=Changelog=
 
;Version 10.0
* Updated: Results in the {{Code|rest}} namespace will be returned unprefixed.
* Updated: {{Code|dir}} elements are returned when listing the contents of a database.
;Version 9.0
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu