Database Reference
In-Depth Information
“Building Applications with RESTXQ” on page 215 covers the specifics of building
REST APIs with RESTXQ, so we will not repeat those here. For the purposes of inte‐
grating your custom RESTXQ REST APIs with other applications or processes, the
main requirement is a decent HTTP client, for which we refer you back to the infor‐
mation in “REST Server API” on page 319 .
With RESTXQ the developer declares a series of HTTP request constraints against an
XQuery function by use of XQuery 3.0 annotations ; the function with constraints is
then known as a resource function . When eXist receives an incoming HTTP request it
checks all of the known resource functions to see if the HTTP request could be serv‐
iced by one of them; if so, the function is executed, and parameters from the HTTP
request may be extracted and injected into the function call as parameters to that
function. The resource function is then (apart from user-defined processing) respon‐
sible for constructing an appropriate HTTP response.
For comparison with the REST Server API, we include with this chapter the XQuery
file restxq-stored-query/image-api.xqm , which is a port of the rest-stored-query/image-
api.xq file, discussed in “Executing stored queries” on page 335 . We hope that this
will aid you in recognizing the different coding styles for using RESTXQ and stored
queries with the REST Server API.
To use the RESTXQ version of image-api.xq , you simply need to store it into any col‐
lection in the database for which RESTXQ is enabled (by default, this is all database
collections apart from those of specific applications in subcollections of /db/apps that
have chosen to disable RESTXQ). Also, you need to ensure that: 1) the calling user
has execute access within the database to the image-api.xqm file, so that it may be
executed; 2) the collection /db/images exists and is writable by the calling user; and 3)
the Image XQuery extension module is enabled in $EXIST_HOME/conf.xml .
Recall from “Executing stored queries” on page 335 that the custom image-api REST
API performs these three functions:
• Stores a JPEG image received over HTTP into the database
• Retrieves a stored image from the database
• Retrieves a thumbnail representation of an image from the database
Let's now look at the XQuery code in detail, and how it performs each of these
functions.
Store a JPEG image received over HTTP into the database
The API provided by the image-api.xqm file allows you to send an HTTP POST to it
via the RESTXQ API to store a JPEG image. In your HTTP request, if you set the
Content-Type to image/jpeg and include the content of a JPEG image in the body of
Search WWH ::




Custom Search