Difference between revisions of "Request Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 33: Line 33:
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
|
+
|The following RESTXQ function can be called via <code><nowiki>http://localhost:8984/restxq/id</nowiki></code>:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
module namespace test = 'http://basex.org/examples/test';
 
module namespace test = 'http://basex.org/examples/test';

Revision as of 22:24, 9 July 2012

This XQuery Module contains functions for processing servlet request data with the RESTXQ API. Note that, for now, the proposed functions are specific to BaseX and are not described in the existing RESTXQ documentations.

Conventions

All functions in this module are assigned to the http://exquery.org/ns/restxq/Request namespace, which must be dynamically imported. In this documentation, the request prefix is used. Next, the first argument of all functions must be a servlet request container, bound via the %restxq:request(...) annotation. An example:

module namespace test = 'http://basex.org/examples/test';

import module namespace request = "http://exquery.org/ns/restxq/Request";

declare
  %restxq:GET
  %restxq:path("...")
  %restxq:request("{$request}")
  function test:function($request)
{
  ...
};

Functions

request:session-id

Signatures request:session-id($request as basex:request) as xs:string
Summary Returns the session ID of a servlet request.
Examples The following RESTXQ function can be called via http://localhost:8984/restxq/id:
module namespace test = 'http://basex.org/examples/test';
import module namespace request = "http://exquery.org/ns/restxq/Request";
declare
  %restxq:path("/id")
  %restxq:request("{$req}")
  function test:id($req)
{
  'ID: ' || request:session-id($req)
};

request:get-attribute

Signatures request:get-attribute($request as basex:request, $key as xs:string) as xs:string?
Summary Returns the value of an attribute bound to the current session, or an empty sequence if no value was bound.
Examples
module namespace test = 'http://basex.org/examples/test';
import module namespace request = "http://exquery.org/ns/restxq/Request";
declare
  %restxq:path("/get")
  %restxq:query-param("key", "{$key}", "")
  %restxq:request("{$req}")
  function test:get($req, $key)
{
  'Value of ' || $key || ': ' || request:get-attribute($req, $key)
};

request:set-attribute

Signatures request:set-attribute($request as basex:request, $key as xs:string, $value as xs:string) as empty-sequence()
Summary Binds an attribute with the specified value to the current session.
Examples
module namespace test = 'http://basex.org/examples/test';
import module namespace request = "http://exquery.org/ns/restxq/Request";
declare
  %restxq:path("/set")
  %restxq:query-param("key", "{$key}", "")
  %restxq:query-param("value", "{$value}", "")
  %restxq:request("{$req}")
  function test:set($req, $key, $value)
{
  request:set-attribute($req, $key, $value),
  'Attribute was set.'
};

Changelog

This module was introduced with Version 7.3.1.