Database Reference
In-Depth Information
the request, it will be stored into the database and
image-api.xqm
will return a
Loca
tion
and
identifier
in the HTTP response for the image.
Consider the following example, where we use cURL to make a request to a RESTXQ
resource function that stores a JPEG image into the database:
curl -i -X POST -H
'Content-Type: image/jpeg'
--data-binary @/tmp/cats.jpg
http://localhost:8080/exist/restxq/image
Let's look at how the code in our
image-api.xqm
stored query handles this request:
declare
%rest:POST ("{$image-data}")
%rest:path("/image")
%rest:consumes("image/jpeg")
function ii:store-image($image-data) {
let $image-name := util:uuid() || ".jpg"
let $db-path :=
xmldb:store($ii:image-collection, $image-name, $image-data,
"image/jpeg")
let $uri-to-resource := rest:uri() || "/" || $image-name
return
(
<rest:response>
<http:response status="{$ii:HTTP-CREATED}">
<http:header name="Location" value="{$uri-to-resource}"/>
</http:response>
</rest:response>
,
<identifier>{$image-name}</identifier>
)
};
We declare that we are only interested in processing HTTP
POST
requests.
We request to have the body of the
POST
request extracted into the function
parameter
$image-data
.
We declare that we are only interested in processing HTTP requests that have a
URI (relative to the RESTXQ API) of
/image
.
We declare that we are only interested in consuming HTTP requests that have a
Content-Type
of
image/jpeg
.
The
$image-data
will receive the body of the HTTP
POST
when the function is
executed, as we declared in
.
Search WWH ::
Custom Search