Database Reference
In-Depth Information
We want to be good REST citizens, so we set the response status to 201 Created ,
as we have just created the resource given to us in the database.
When creating a resource, REST calls for the Location header in the response to
be set with a URI to the new resource, so we do that.
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.
If the Content-Type of the request was not image/jpeg , we do not wish to pro‐
cess the request, so we set the response status to 400 Bad Request .
Retrieve a stored image from the database. The API provided by the image-api.xq file
allows you to send an HTTP GET to it via the REST Server API to get a previously
stored image. In your HTTP request, if the URI includes an identifier of an image
previously stored by the API, then it will return the content of that image.
When you make the following request with cURL ( 28068cd4-4817-4f81-
ae19-5ad2c945186a.jpg is the identifier of the image returned by the API when we
stored it in the previous section):
curl http://localhost:8080/exist/rest/db/image-api.xq
/28068cd4-4817-4f81-ae19-5ad2c945186a.jpg
the code in our image-api.xq stored query handles it like so:
else if ( request:get-method () eq "GET" ) then
(: NOTE: thumbnail part is dealt with in the next section! :)
else if ( matches (
request:get-uri (),
concat ( ".*/" , $ local:uuidv4-pattern , "\.jpg$" )
)) then
let $ image-name := tokenize ( request:get-uri (), "/" )[ last ()] ,
$ image := local:get-image ( $ image-name )
return
if ( not ( empty ( $ image ))) then
response:stream-binary ( $ image , "image/jpeg" , $ image-name )
else
(
response:set-status-code ( $ response:NOT-FOUND ),
<image-not-found> { $ image-name } </image-not-found>
)
We examine the request to see if it is an HTTP GET request, using the
request:get-method function.
Search WWH ::




Custom Search