Thijs
Thijs

Reputation: 1546

REST API return document contents

I try to return all documents by cts:uri-match. Not only the uri's but the entire documents. So I've put some documents in /app/customer/ and now I create this REST API extension endpoint. But when I can't get this to return the actual documents.

It looks like my query is working ("produced 6 results") but the output format is wrong.

This is my error:

<rapi:error 
    xmlns:rapi="http://marklogic.com/rest-api">
    <rapi:status-code>400</rapi:status-code>
    <rapi:status>Bad Request</rapi:status>
    <rapi:message-code>RESTAPI-INVALIDRESULT</rapi:message-code>
    <rapi:message>RESTAPI-INVALIDRESULT: (err:FOER0000) Invalid result:  reason: GET     extension produced 6 results and 1 mime types: customers</rapi:message>
</rapi:error>

This is my extension:

xquery version "1.0-ml";

module namespace foo = "http://marklogic.com/rest-api/resource/customers";
declare namespace roxy = "http://marklogic.com/roxy";
declare namespace pub = "http://acme.com/foo/publisher/1.0";

declare 
%roxy:params("id=xs:string")

function foo:get(
    $context as map:map,
    $params  as map:map
) as document-node()*
{
    map:put($context, "output-types", "application/xml"),
    xdmp:set-response-code(200, "OK"),
    foo:getCustomersByXPath($params)
};

declare function foo:getCustomersByXPath(
    $params  as map:map
) as document-node()* {
    let $set := cts:uri-match("/app/customer/*")
    let $id := map:get($params,"id")
    for $x in doc($set)
    return doc($x)            
};

To check the data in the directory:

http://myserver:myport/v1/search?q=&directory=/app/customer/

Results:

<search:response snippet-format="snippet" total="7" start="1" page-length="10" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=""  xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/app/customer/10848614934359542547.xml" path="fn:doc(&quot;/app/customer/10848614934359542547.xml&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F10848614934359542547.xml" mimetype="text/xml" format="xml">
    <search:snippet/>
  </search:result>
  <search:result index="2" uri="/app/customer/7883534461919564626.json" path="fn:doc(&quot;/app/customer/7883534461919564626.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F7883534461919564626.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="3" uri="/app/customer/10893316875648096464.json" path="fn:doc(&quot;/app/customer/10893316875648096464.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F10893316875648096464.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="4" uri="/app/customer/11529967549112309613.json" path="fn:doc(&quot;/app/customer/11529967549112309613.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F11529967549112309613.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="5" uri="/app/customer/12616183128326713409.xml" path="fn:doc(&quot;/app/customer/12616183128326713409.xml&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F12616183128326713409.xml" mimetype="text/xml" format="xml">
    <search:snippet/>
  </search:result>
  <search:result index="6" uri="/app/customer/2938594927859036749.json" path="fn:doc(&quot;/app/customer/2938594927859036749.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F2938594927859036749.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="7" uri="/app/customer/1602860626261524046.json" path="fn:doc(&quot;/app/customer/1602860626261524046.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F1602860626261524046.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:qtext/>
  <search:metrics>
    <search:query-resolution-time>PT0.005074S</search:query-resolution-time>
    <search:facet-resolution-time>PT0.000077S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0.000938S</search:snippet-resolution-time>
    <search:total-time>PT0.182643S</search:total-time>
  </search:metrics>
</search:response>

Upvotes: 0

Views: 1205

Answers (1)

ehennum
ehennum

Reputation: 7335

A get() extension function can return multiple documents, which can have different mime types.

Try setting the output-types key to a sequence with one mime type string for each returned document. In your case, it might be the case that each string is "application/xml"

Hoping that helps,

Erik Hennum

Upvotes: 3

Related Questions