Madoantony
Madoantony

Reputation: 195

How to include Marklogic rest-api in custom rewriter code in Xquery?

I'm new to url rewriting process in Marklogic and need help to resolve the below issue.

I have written Xquery implementation to redirect my API endpoints to the respective Xquery modules as /rewriter-ex/rewriter.xqy.

xquery version "1.0-ml";

let $url := xdmp:get-request-url()

 return if(fn:matches($url,"/fetchRecord")) then
             fn:replace($url,"/fetchRecord","/lib/fetch-record.xqy$1")
        else if(fn:matches($url,"/saveRecord")) then
             fn:replace($url,"/saveRecord$","/lib/save-record.xqy")
        else (xdmp:set-response-code(404, "Not found"),"/no/such/resource")

And the url-rewriter path in the App server configuration is set to /rewriter-ex/rewriter.xqy and rewrite resolves globally parameter is set to true in App server.

I'm able to redirect my API urls to the respective endpoints.But I'm not able to use predefined ML Res-API endpoints like /v1/documents,it is showing 404 error as returned in the rewriter.xqy.

Is there a way I can implement rewriter to support both rest api endpoints as well as custom API endpoints?

Upvotes: 2

Views: 269

Answers (3)

Madoantony
Madoantony

Reputation: 195

Thanks for your answers.I'm planning to use two app servers one for rest calls and other for API calls.These two app servers will point to the same DB.Please let me know if this is a right approach.

Upvotes: 0

Charles Foster
Charles Foster

Reputation: 338

If you'd like to create your own RESTful API on top of MarkLogic, with your own custom end-points. Please check out XQuery API for RESTful Services (XQRS).

declare
  %rest:path("/fetchRecord/{$record-id}")
  %rest:GET
function fetch-record($record-id as xs:string) {
  fn:doc($record-id)
};

declare
  %rest:path("/saveRecord/{$record-id}")
  %rest:PUT("{$doc}")
  %xdmp:update
function put-record($record-id as xs:string, $doc as document-node(element())) {
  xdmp:document-insert($record-id, $doc)
};

Your RESTXQ Modules can sit on their own separate HTTP App Server (on their own port) and live side by side with another HTTP App Server which has the default MarkLogic REST API on it.

XQRS is totally compatible with the rest of MarkLogic's software, including Data Hub Framework, they can party together.

Upvotes: 1

ehennum
ehennum

Reputation: 7335

The REST API doesn't support customization or replacement of the default declarative rewriter configured during initialization of a REST API server. In addition, the REST API doesn't provide an XQuery API interface to the functionality of the REST API endpoints.

Instead, the recommended approach is to extend the REST API through one of the following alternatives:

Hoping that helps,

Upvotes: 0

Related Questions