Database Reference
In-Depth Information
Store a JPEG image received over HTTP into the database.
The API provided by the
image-
api.xq
file allows you to send an HTTP
POST
to it via the REST Server 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 the request, it will be stored into
the database and
image-api.xq
will return a
Location
and
identifier
in the HTTP
response for the image.
When you make the following request with cURL:
curl -i -X POST -H
'Content-Type: image/jpeg'
-data-binary @/tmp/cats.jpg
http://localhost:8080/exist/rest/db/image-api.xq
the code in our
image-api.xq
stored query handles it as follows:
if
(
request:get-method
()
eq
"POST"
)
then
if
(
request:get-header
(
"Content-Type"
)
eq
"image/jpeg"
)
then
let
$
db-path
:=
local:store-image
(
request:get-data
())
,
$
uri-to-resource
:=
concat
(
request:get-uri
(),
substring-after
(
$
db-path
,
$
local:image-collection
))
return
(
response:set-status-code
(
$
response:CREATED
)
,
response:set-header
(
"Location"
,
$
uri-to-resource
)
,
<identifier>
{
substring-after
(
$
db-path
,
concat
(
$
local:image-collection
,
"/"
))
}
</identifier>
)
else
response:set-status-code
(
$
response:BAD-REQUEST
)
We examine the request to see if it is an HTTP
POST
request, using the
request:get-method
function.
We check that the
Content-Type
header was set to
image/jpeg
, as we only want
to work with JPEG images in this example; if not, skip to
.
We call the function
local:store-image
on the body of the
POST
request, which
we obtained using the
request:get-data
function. This function has been omit‐
ted for brevity, but all you need to know right now is that it stores the image into
the database, and returns a path to the image in the database.
We create a URI for our newly stored image, based on the current URI of our
API, which we can find by using
request:get-uri
and some substring of the
path to the image in the database.
Search WWH ::
Custom Search