REST
This page is part of the Developer Section. It describes how to use the JAX-RX API of BaseX.
BaseX offers a RESTful API for accessing distributed XML resources. REST (REpresentational State Transfer) facilitates a simple and fast access to databases through HTTP. The HTTP methods GET, PUT, DELETE, and POST can be applied to interact with the database.
The REST implementation has formerly been based on JAX-RX, an interface layer to provide unified access to XML databases and resources. With Version 6.8, it has been replaced with a native REST implementation that allows a much closer integration with the XQuery, WebDAV, and other features of BaseX.
Contents
Starting HTTP server
First of all, please launch a HTTP Server instance of BaseX: please run the basexhttp
script, or double click on the BaseX Server (Start) start icon in Windows. Follow this link for some more information (or check out the additional command-line options).
Most browsers can be used to directly display the results of REST requests. Some more alternatives how to use REST are listed in the Usage Examples Paragraph.
URL Architecture
The root URL lists all available databases. The following examples assume that you have created a database instance from the factbook.xml document:
<rest:databases resources="1" xmlns:rest="http://www.basex.org/rest"> <rest:database resources="1" size="1813599">factbook</rest:database> </rest:databases>
The resources of a database can be listed by specifying the database, and potential sub directories, in the URL. In the given example, a single document is stored in the factbook database:
<rest:database name="factbook" resources="1" xmlns:rest="http://www.basex.org/rest"> <rest:resource type="xml" size="77192">factbook.xml</rest:resource> </rest:database>
The contents of a database can be accessed by adding the query parameter to the path:
If a resource is not found, an HTTP response will be generated with 404
as status code.
Query Parameters
GET and POST requests can be extended with a number of parameters. Only one of the following operations can be specified:
query
:
Evaluates an XPath/XQuery expression.
If a database or database path is specified in the URL, it is used as query context.run
:
Runs a query file located on the server.
The query directory is defined by theHTTPPATH
option.command
:
Executes a database command.
The following parameters can be applied to the query
and run
operations:
wrap
:
Wraps the results in XML elements (default:no
).- Variables:
All query parameters prefixed with a dollar sign ($
) will be treated as external variables. The parameter name and value will be bound to the query before it is evaluated. - Serialization:
All serialization parameters known to BaseX can be specified as query parameters. Parameters that are specified within a query will be interpreted by the REST server before the output is generated.
Request Methods
GET Requests
Using GET, all query parameters can be directly specified within the URL. The following example prints the city names from the factbook database:
The next request chooses US-ASCII
as output encoding and opens the database within the XQuery expression:
The next URL turns on XML wrapping and lists all database users registered in BaseX:
POST Requests
The POST method offers two different operations:
Add New Resources
By default, the HTTP request body will be added as new XML document to the specified
database. For example, if a document is sent as body of the POST request to the URL
localhost:8984/rest/DB/file.xml
, the document will be added to the
DB
database, and the 201 (Created) status code will be returned to
confirm that everything went alright.
Execute Queries & Commands
If application/query+xml
is chosen as content type, the HTTP request
body is interpreted as query. The body must conform to the REST POST Schema.
The output of the following query returns the five first city names of the factbook database:
<query xmlns="http://www.basex.org/rest"> <text><![CDATA[ (//city/name)[position() <= 5] ]]></text> </query>
The following POST request prints the registered database users in the specified ISO-8859-1
encoding:
<command xmlns="http://www.basex.org/rest"> <text>show users</text> <parameter name='encoding' value='ISO-8859-1'/> </command>
PUT Requests
The PUT method can be used to create or update a database resource.
- Usage
- Use PUT to send the URL and upload the input XML document.
- Example
- A new database with the name XMark is created if the URL
localhost:8984/rest/XMark
is sent via PUT, followed by the input XML file in the HTTP body. The document will have the same name as the database.
If the process was successful, a HTTP response with status code 201
(CREATED) is sent back.
Otherwise, 404
will be sent.
DELETE Requests
The DELETE method can be applied to delete single resources.
- Usage
- Use DELETE to send the URL pointing to the database or resource to be deleted.
- Example
- The factbook database is deleted via the DELETE method and the URL
localhost:8984/rest/factbook
.
If deletion was successful, the HTTP status code 200
(OK) will be sent.
If not, 404
is returned.
Assigning Variables
GET Requests
Query parameters prefixed with a dollar sign ($
) will be handled as external variables:
Query:
declare variable $x as xs:integer external; $x
Variables:
- Assign the variable with:
&$x=21
Complete request (compact notation, omitting the explicit variable declarations):
POST Requests
Using POST, the <variable/>
element is used to bind an external variable to a query:
<query xmlns="http://www.basex.org/rest"> <text> declare variable $x as xs:integer external; declare variable $y as xs:integer external; $x * $y </text> <variable name="x" value="21"/> <variable name="y" value="2"/> </query>
User Management
By default, the HTTP server is started with no predefined user. Users and passwords can be sent via HTTP basic access authentication with each HTTP request. As an alternative, users and passwords can also be specified as command-line arguments or via the "user.basex.user" and "user.basex.password" system properties before the HTTP server is started.
With some browsers and with cURL, you can send specify the user name and password with each HTTP request within the request string as plain text, using the format USER:PASSWORD@URL
. An example:
- GET
curl -i "bob:alice@localhost:8984/rest/factbook"
Content Type
As the correct result content type cannot always be dynamically determined, the user can manually adjust the content type. The final content type of a REST response is chosen in several steps:
- By default, the content type of a response depends on the chosen operation:
- Command results are returned as
text/plain
- Query results are returned as
application/xml
- Command results are returned as
- The default content type is overwritten if a serialization method is specified, either as query parameter or within the XQuery expression. The following methods are available:
xml
→application/xml
xhtml
→text/html
html
→text/html
text
→text/plain
raw
→application/octet-stream
json
orjsonml
→application/json
- The content type is overwritten in any case if a specific media-type is chosen, again as query parameter or within the query.
The following three example requests will all return <a/>
as result and use application/xml
as content-type:
http://admin:admin@localhost:8984/rest?query=%3Ca/%3E
http://admin:admin@localhost:8984/rest?query=%3Ca/%3E&method=xml
http://admin:admin@localhost:8984/rest?query=%3Ca/%3E&media-type=application/xml
Usage Examples
Java
Most programming languages offer libraries to communicate with HTTP servers. The following example demonstrates how easy it is to perform a DELETE request with Java.
Basic access authentication can be activated in Java by adding an authorization header
to the HttpURLConnection
instance. The header contains the word
Basic
, which specifies the authentication method, followed by the
Base64-encoded USER:PASSWORD
pair. As Java does not include a default
conversion library for Base64 data, the internal BaseX class
org.basex.util.Base64
can be used for that purpose:
// The java URL connection to the resource. URL url = new URL("http://admin:admin@localhost:8984/rest/factbook"); // Establish the connection to the URL. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Set as DELETE request. conn.setRequestMethod("DELETE"); // User and password. String user = "bob"; String pw ="alice"; // Encode user name and password pair with a base64 implementation. String encoded = Base64.encode(user + ":" + pw); // Basic access authentication header to connection request. conn.setRequestProperty("Authorization", "Basic " + encoded); // Print the HTTP response code. System.out.println("\n* HTTP response: " + conn.getResponseCode()); // Close connection. conn.disconnect();
Find Java examples for all methods here: GET, POST (Add), POST (Query), PUT, DELETE.
Command Line
Next, tools such as the Linux command cURL exist to perform HTTP requests (try copy & paste):
- GET
curl -i "admin:admin@localhost:8984/rest/factbook?query=//city/name&count=5"
- POST (Add)
curl -i -H "Content-Type: text/xml" -d "<HelloWorld/>" "admin:admin@localhost:8984/rest/world/hello.xml"
- POST (Query)
curl -i -X POST -H "Content-Type: application/query+xml" -d
"<query xmlns='http://www.basex.org/rest'><text>//city/name</text><parameter name='count' value='5'/></query>"
"admin:admin@localhost:8984/rest/factbook"curl -i -X POST -H "Content-Type: application/query+xml" -T query.xml "admin:admin@localhost:8984/rest/factbook"
- PUT
curl -i -X PUT -T "etc/xml/factbook.xml" "admin:admin@localhost:8984/rest/factbook"
- DELETE
curl -i -X DELETE "admin:admin@localhost:8984/rest/factbook"