1. Basic MarkLogic Server Setup

  1. In the MarkLogic Admin Console, create a new HTTP Server
  2. Download xqrs.xqy and place it in your Modules directory or Modules database
  3. Set the HTTP App Server property "url rewriter" to point to the xqrs.xqy file

2. Create a simple RESTXQ Module

The following example uses the prefix example with a URI of http://www.example.org/example, but you can pick whatever you like for either of these.

xquery version "1.0-ml";

module namespace example = "http://www.example.org/example";
declare namespace rest = "http://exquery.org/ns/restxq";

declare
  %rest:path("/hello-world")
function hello-world() {
  "Hello World"
};

declare
  %rest:path("/echo/{$param}")
function echo($param as xs:string) {
  $param
};

3. Register the RESTXQ Module

To register the RESTXQ Module with the XQRS framework you must import it into the xqrs.xqy main module. You'll find an XQuery comment near the top of the file showing where you can place these imports.

xquery version "1.0-ml";

(: ------------ Add your own RestXQ module library imports here ------------ :)
import module namespace example = "http://www.example.org/example" at
  "example.xqy"; (: Our First RESTXQ Module :)

import module namespace sem = "http://marklogic.com/semantics"
  at "/MarkLogic/semantics.xqy"; (: Don't delete this import :)

You can create as many RESTXQ Modules as you like and add them here.

4. Test it's all working

With the above code setup correctly, in your browser you should be able to navigate to the URL address "/hello-world" and see the plain text response of

Hello World

Also, pointing your browser to "/echo/whasup" should return the plain text response of

whasup

Any request that does not match a function's criteria will be passed directly through to the MarkLogic App Server itself as if no url rewriter was in place, so you can serve images and other media files directly from their normal paths as you would with a normal Web App or even include other existing .xqy files that can live side by side with your new RESTXQ Modules.

TOP