XQuery/Getting POST Data

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

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

Method[edit | edit source]

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

Sample echo-post.xq[edit | edit source]

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>
   {$post-data}
</post-data>

Discussion[edit | edit source]

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 Data[edit | edit source]

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.

sample_postdata.xq

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)
return
<results>
  {$data-to-save}
</results>

Viewing URL Encoded Parameters[edit | edit source]

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"

xquery version "1.0";

let $title := 'Echo Post'

return
<results>
  <title>{$title}</title>
  <get-data>
  {request:get-data()}
  </get-data>
  <headers>
     {for $header in request:get-header-names()
     return
        <header name="{$header}" value="{request:get-header($header)}"/>
  }
  </headers>
  
  <parameters>
  {for $parameter in request:get-parameter-names()
     return
        <parameter name="{$parameter}" value="{request:get-parameter($parameter, '')}"/>
  }
  </parameters>
</results>

If you have the following form:

<html>
    <head><title></title></head>
    <body>   
        <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" />
        </form>    
    </body>
</html>

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

<results>
    <title>Echo Post</title>
    <get-data/>
    <headers>
        <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:1.9.2.23) 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"/>
    </headers>
    <parameters>
        <parameter name="FirstName" value="Mickey"/>

        <parameter name="LastName" value="Mouse"/>
    </parameters>
</results>