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 http://localhost:8984/rest?run=t1.xq 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
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!" };
[cid:image001.png@01D82714.E1AB03A0] 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:
* Add your existing modules to BaseX using normal module deployment mechanisms * Create RESTXQ modules in the webapp directory that then call your modules as appropriate.
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 servicenow.comhttps://www.servicenow.com LinkedInhttps://www.linkedin.com/company/servicenow | Twitterhttps://twitter.com/servicenow | YouTubehttps://www.youtube.com/user/servicenowinc | Facebookhttps://www.facebook.com/servicenow
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__;!!N4vogdj...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
Hi Eliot, Thanks for the response. I will very likely switch to a RESTXQ API once I'm confident BaseX will do what I need, but after some more tinkering including trying the RESTXQ solution you suggested, I think there's an issue with how the REST API resolves URIs for module import. I set the RESTPATH option to something clearly outside the BaseX install directory (/tmp) and reran my original test. Even though t1.xq is importing module t2 using the path "t2.xq", BaseX is reporting it can't find the module t2 in its home directory, not the repo directory nor the RESTPATH directory. That suggests it's not resolving the relative URI correctly.
I then tried a RESTXQ style request with RESTXQPATH set outside the BaseX directory as described the reply and it behaved as I expected in terms of resolving the import. (It did not, however, look in the repo directory but in the directory containing t1.xq, which I believe is consistent with the XQuery specification.)
That's definitely encouraging, as I at least feel like I understand what's happening. If one of the developers would like to weigh in before I submit an issue, I'd appreciate it.
As for the naming conventions, point taken. The original code has a 20 year history and will require some modernization.
Thanks!
-- Paul
On Feb 21, 2022, at 12:19 PM, Eliot Kimber eliot.kimber@servicenow.com wrote:
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!" };
<image001.png> 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:
• Add your existing modules to BaseX using normal module deployment mechanisms • Create RESTXQ modules in the webapp directory that then call your modules as appropriate.
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 servicenow.com 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__;!!N4vogdj... 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
Hi Paul,
Thanks for the valuable hint. I agree that’s a bug. A surprising one for me, actually, I’m not sure when the base URI got lost. It should be correctly assigned again, and refer to the currently evaluated module [1,2]. BaseX 9.7 is due later this week.
Best, Christian
[1] https://files.basex.org/releases/latest/ [2] https://github.com/BaseXdb/basex/commit/f33e1c44a576919511b0a91bc8863fa465a6...
On Mon, Feb 21, 2022 at 3:58 PM Paul L. Merchant Jr. Paul.L.Merchant.Jr@dartmouth.edu wrote:
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 http://localhost:8984/rest?run=t1.xq 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
basex-talk@mailman.uni-konstanz.de