You need to turn your scripts into modules with functions that have the necessary RESTXQ annotations—it won’t work to just drop normal XQuery scripts into the webapps directory.
So for your t2.xq file (which should be named t2.xqm per BaseX’s conventions for naming modules), you would have something like:
module namespace t2="t2";
declare
%rest:GET
%rest:path('/t2')
%output:method('html')
function t2:response() as xs:string {
"Hello World!"
};
Which should be served at localhost:9894/t2 in the default configuration. The module t2.xq would not be callable directly unless you also deployed it to the repo dir.
So the general approach for migrating existing code to BaseX’s RESTXQ implementation would be:
The built-in BaseX web apps serve as useful examples of RESTXQ implementation.
As for setting a cookie—for web pages you’re creating normal HTML pages so you can include whatever JavaScript you need to do whatever one can do in a browser. The BaseX DBA web application demonstrates how
to do this.
Cheers,
E.
_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
LinkedIn | Twitter | YouTube | Facebook
From:
BaseX-Talk <basex-talk-bounces@mailman.uni-konstanz.de> on behalf of Paul L. Merchant Jr. <Paul.L.Merchant.Jr@dartmouth.edu>
Date: Monday, February 21, 2022 at 8:58 AM
To: basex-talk@mailman.uni-konstanz.de <basex-talk@mailman.uni-konstanz.de>
Subject: [basex-talk] REST API
[External Email]
Hi everyone, I have a couple of questions about BaseX's REST API and how it executes stored XQuery. I'm evaluating how much effort would be involved in porting an existing XQuery code base to BaseX. The existing code is a directory of XQuery scripts and
modules that are called over a REST interface. They aren't RESTXQ aware, although with some work they could be converted if needed. (I'd prefer to do as little modification as possible initially, however.)
I've installed BaseX 9.6.4 on Mac OS in a directory I'll call BASEX_HOME. In $BASEX_HOME/.basex, I have WEBPATH set to $BASEX_HOME/webapp (the default) and RESTPATH = "" (the default). I also set an appropriate user and password. I've placed the following
two scripts in $BASEX_HOME/webapp:
--------
t1.xq
--------
import module namespace t2="t2" at "t2.xq";
t2:response()
--------
t2.xq
--------
module namespace t2="t2";
declare function t2:response() as xs:string {
"Hello World!"
};
--------
When I run the first script using curl, I get an error back that the second cannot be located in the $BASEX_HOME directory not $BASEX_HOME/webapp as I expected:
$ curl
https://urldefense.com/v3/__http://localhost:8984/rest?run=t1.xq__;!!N4vogdjhuJM!RUXSNPKPPWsMPAVeb7bedIxQ5YCBHCwngZEYbABfJSfFjRg6l3bMVYAsG0X35LKQu3iimQ$
Stopped at ., 3/1:
[XQST0059] Could not retrieve module: $BASEX_HOME/t2.xq.
Is this the expected behavior, or is BaseX resolving the path to module t2 incorrectly? In any case given this behavior, if I want to point RESTPATH outside the BaseX installation to a much larger library of XQuery, how can I share modules within the library
without having to use absolute paths for everything?
My second question is simply, is it possible to set a cookie in response to a REST (not RESTXQ) response?
Thanks!
-- Paul