XQuery/Getting POST Data

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

Method
To do this you use the  XQuery function.

Sample echo-post.xq
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

return  {$post-data} 

Discussion
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  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 Data
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:



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

sample_postdata.xq

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

URL Encoded data has the following mime-type:

Content-Type="application/x-www-form-urlencoded"

If you have the following form:

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