Database Reference
In-Depth Information
We call the function
xmldb:store
on the body of the
POST
request. This function
stores the image into the database, and returns a path to the image in the data‐
base.
We construct a public, dereferenceable URI to the stored image. Of particular
interest here is the call to
rest:uri
, which gives us the absolute URI of the exe‐
cuting resource function.
The response of the function will be a sequence, where the first item will instruct
RESTXQ about the HTTP response and the second item will be the body of the
HTTP response.
We instruct RESTXQ to set the HTTP response code to
201 Created
and add an
HTTP header declaring a URI to the location of the stored image.
As an added bonus, we also return an identifier for the created resource in the
body of the response; this identifier may then be used in subsequent requests to
the API.
Retrieve a stored image from the database
The API provided by the
image-api.xqm
file allows you to send an HTTP
GET
to it via
the RESTXQ API to get a previously stored image. If the URI in your HTTP request
includes an identifier of an image previously stored by the API, then it will return the
content of that image.
Consider the following example, where we use cURL to make a request to a RESTXQ
resource function that returns an image from the database:
curl http://localhost:8080/exist/rest/db/image/
24a85a52-5031-4bac-8843-4c7e7701905b.jpg
24a85a52-5031-4bac-8843-4c7e7701905b.jpg
is the identifier of
the image returned by the API when we stored it in the previous
section.
Let's look at how the code in our
image-api.xqm
stored query handles this request:
declare
%rest:GET
%
rest:path
(
"/image/{$image-name}"
)
%
rest:produces
(
"image/jpeg"
)
%
output:method
(
"binary"
)
function
ii:get
-
image-rest
(
$
image-name
)
{
let
$
image
:=
ii:get-image
(
$
image-name
)
Search WWH ::
Custom Search