Database Reference
In-Depth Information
URI path constraint annotation
A resource function may have a path constraint annotation , %rest:path , as shown in
Example 9-7 . A path constraint annotation constrains the URI path of an HTTP
request that a resource function may process. The URI path may itself contain tem‐
plates that are extracted and injected as parameters to the function. A URI path con‐
straint may not be used by itself; it always requires at least one HTTP method
constraint annotation to also be present on the resource function. The URI path is
always relative to the base URI of the RESTXQ Server (see “Configuring RESTXQ”
on page 216 ).
Example 9-7. Resource function saying hello with URI templating
xquery version "3.0";
module namespace ex = "http://example/restxq/4";
import module namespace rest = "http://exquery.org/ns/restxq";
declare
%rest:GET
%rest:path("/hello/{$name}")
function ex:say-hello($name) {
<greeting>Hi there {$name}!</greeting>
};
We declare that we wish to only process paths that start with /hello followed by a
path segment template, which should be extracted and injected into the function
parameter $name .
This parameter will be set to the value of the URI template declared by
%rest:path when invoked by RESTXQ.
The value of the path segment will be output as part of the response.
This simple example declares the URI path to service, extracts a single URI segment
using templating, and returns a result showing the value extracted from the URI.
By storing the XQuery anywhere in the database and granting it execute rights, you
may then access it by an HTTP GET to the URI http://localhost:8080/exist/restxq/hello/
myName . For example, using cURL:
$ curl http://localhost:8080/exist/restxq/hello/Liz
<greeting>Hi there Liz!</greeting>
Note the name has been extracted from the URI; changing the last segment of the
URI changes the greeting!
Search WWH ::




Custom Search