Request Module

From BaseX Documentation
Revision as of 07:48, 7 September 2012 by CG (talk | contribs)
Jump to navigation Jump to search

This XQuery Module contains functions for retrieving information on an HTTP request that has triggered the query. It is mainly useful in combination with the RESTXQ API. Note that all functions will also be defined in the upcoming EXQuery Request Module.

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 namespace is bound to the request prefix.

Functions

request:parameter-names

Signatures request:parameter-names() as xs:string*
Summary Returns the names of all query parameters contained in the HTTP request. This function can be used to find or check query parameters that have not been bound by %restxq:query-param annotations.

request:parameter

Signatures request:parameter($name as xs:string) as xs:string*
Summary Returns all strings bound to the query parameter specified by $name.
Examples The following RESTXQ function can e.g. be called via http://localhost:8984/restxq/get?key=user:
module namespace test = 'http://basex.org/examples/test';
import module namespace request = "http://exquery.org/ns/restxq/request";
declare
  %restxq:path("/get")
  function test:get()
{
  'Value of parameter "key": ' || request:parameter('key')
};

request:session-id

Signatures request:session-id() 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")
  function test:id()
{
  'ID: ' || request:session-id()
};

request:attribute

Signatures request:attribute($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 The following RESTXQ function can e.g. be called via http://localhost:8984/restxq/get?key=user:
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:update-attribute

Signatures request:update-attribute($key as xs:string, $value as xs:string) as empty-sequence()
Summary Binds an attribute with the specified value to the current session.
Examples The following RESTXQ function can e.g. be called via http://localhost:8984/restxq/set?key=user&value=john:
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.'
};

request:path

Signatures request:path() as xs:string
Summary Returns the path relative to the RESTXQ URI.
Examples The following RESTXQ function can e.g. be called via http://localhost:8984/restxq/hey/john:
module namespace test = 'http://basex.org/examples/test';
import module namespace request = "http://exquery.org/ns/restxq/request";
declare
  %restxq:path("/hey/{$name}")
  %restxq:request("{$req}")
  function test:get($req, $name)
{
  'Hey ' || $name || '! Relative path of this page: ' || request:path($req)
};

Changelog

This module was introduced with Version 7.5.