Search code examples
restmarklogic

Output content-type and other headrs in a REST Extension GET written in XQuery


I put a question about this but I associate it with a filter document problem. It isn't, I simplify the sample to maximum and I have always the same content-type and more. The result is always the same, no new headers fields and generic content-type

xquery version "1.0-ml";
module namespace he.test = "http://marklogic.com/rest-api/resource/he.test";

declare function he.test:get($context as map:map,$params as map:map) as document-node() {
    let $doc := doc('/LS/MEDIA/test')
    let $_ := map:put($context, "output-type",'image/jpeg')
    let $_ := map:put($context,"ouput-headers",("Marc","value1","marc2","value2"))
    let $_ := map:put($context, "content-type",'image/jpeg')
return
$doc
};

this query file is deploy with ml-gradle.


Solution

  • @rjrudin is right, the correct key is output-types (plural.) Note you have a typo in output-headers (first t missing.) If you create the following service (using DHF, put it in /src/main/ml-modules/services/xxx.xqy):

    xquery version "1.0-ml";
    module namespace xxx = "http://marklogic.com/rest-api/resource/xxx";
    
    declare function xxx:get($context as map:map, $params as map:map) as document-node()
    {
        map:put($context, 'output-types',   'text/xxx'),
        map:put($context, 'output-headers', ('X-Foo', 'bar')),
        document { binary { xs:hexBinary('3432') } }
    };
    

    Deploy it (e.g. ./gradlew mlDeployApp) and then go to http://localhost:8011/v1/resources/xxx. You get the following answer (note that "42" is just interpreting both octets 0x34 and 0x32 as ASCII text):

    HTTP/1.1 200 OK
    Content-type: text/xxx
    X-Foo: bar
    Server: MarkLogic
    Content-Length: 2
    Connection: Keep-Alive
    Keep-Alive: timeout=5
    
    42