XQuery/Getting POST Data


You want to create an XQuery that will access data in the HTTP POST.


To do this you use the request:get-data() XQuery function.

Sample echo-post.xqEdit

xquery version "1.0";

(: echo-post.xq: Return all data from an HTTP post to the caller. :)

declare namespace exist = "http://exist.sourceforge.net/NS/exist";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";

declare option exist:serialize "method=xml media-type=text/xml indent=yes";

let $post-data := request:get-data()



The program above (called echo-post.xq) is a very useful program for testing your web forms. It just takes the data sent to the XQuery service and returns it wrapped in a <post-data> tag.

Sometimes HTTP POST statements put their data in parameters. For example the RichTextEditor CKEdit has multiple text areas that might each contain HTML markup in encoded forms. In this case you can also use the request:get-parameter on HTTP POST data.

After your server gets a POST from a CKEditor client the server will use the following:

Sample XQuery to Echo Post DataEdit

In the _samples folder you will find several samples of how to use CKEditor. Each of these HTML files has an HTML form with the following line:

  <form action="sample_posteddata.xq" method="post">

The following program can be used as a substitute for the sample_postdata.php file.


xquery version "1.0";
declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=yes indent=yes";
(: Get the content of the editor1 parameter :)
let $editor1 := request:get-parameter('editor1', '')
(: wrap the content in a div to make sure we have well-formed XML :)
let $wrapped-content := concat('&lt;div&gt;', $editor1, '&lt;/div&gt;')
(: parse the escaped text so that we now have true XML markup :)
let $data-to-save := util:parse($wrapped-content)

Viewing URL Encoded ParametersEdit

Standard HTML forms use a data transmission format called URL encoded form data.

URL Encoded data has the following mime-type:


xquery version "1.0";
let $title := 'Echo Post'
     {for $header in request:get-header-names()
        <header name="{$header}" value="{request:get-header($header)}"/>
  {for $parameter in request:get-parameter-names()
        <parameter name="{$parameter}" value="{request:get-parameter($parameter, '')}"/>
If you have the following form:
<source lang="xml">
        <form action="echo-post.xq" method="post">
            First name: <input type="text" name="FirstName" value="Mickey" /><br />
            Last name: <input type="text" name="LastName" value="Mouse" /><br />
            <input type="submit" value="Send HTTP Post to Server" />

Then it will return the following result from the echo-post.xq

    <title>Echo Post</title>
        <header name="Host" value="demo.danmccreary.com"/>
        <header name="User-Agent" value="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv: Gecko/20110920 Firefox/3.6.23"/>
        <header name="Accept" value="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"/>
        <header name="Accept-Language" value="en-us,en;q=0.5"/>
        <header name="Accept-Encoding" value="gzip,deflate"/>
        <header name="Accept-Charset" value="ISO-8859-1,utf-8;q=0.7,*;q=0.7"/>
        <header name="keep-alive" value="115"/>
        <header name="Connection" value="keep-alive"/>
        <header name="Referer" value="http://demo.danmccreary.com/rest/db/dma/apps/xforms-examples/unit-tests/html-form-post.html"/>
        <header name="Content-Type" value="application/x-www-form-urlencoded"/>
        <header name="Content-Length" value="31"/>
        <parameter name="FirstName" value="Mickey"/>
        <parameter name="LastName" value="Mouse"/>
Last modified on 9 November 2011, at 13:23