restmarklogicsjs

Can't able to view a transform in browser using REST in Marklogic 9?


I tried to install below Server-Side JavaScript using this documentation and saved below as rest-sjs

function insertTimestamp(context, params, content)
{
  if (context.inputType.search('json') >= 0) {
    var result = content.toObject();
    if (context.acceptTypes) {                 /* read */
      result.readTimestamp = fn.currentDateTime();
    } else {                                   /* write */
      result.writeTimestamp = fn.currentDateTime();
    }
    return result;
  } else {
    /* Pass thru for non-JSON documents */
    return content;
  }
};

exports.transform = insertTimestamp;

I tried to push this using below curl cmd:

curl --anyauth --user public\admin:admin -X PUT -i --data-binary @"C:/Users/name/Desktop/rest.sjs" -H "Content-type: application/vnd.marklogic-javascript" 'http://localhost:9963/v1/config/transforms/js-example'

When I used localhost:9963 and went to /v1/config/transforms I can see:

<rapi:transforms xmlns:rapi="http://marklogic.com/rest-api">
<rapi:transform>
<rapi:name>rest-tsm</rapi:name>
<rapi:source-format>javascript</rapi:source-format>
<rapi:transform-parameters/>
<rapi:transform-source>/v1/config/transforms/rest-tsm</rapi:transform-source>
</rapi:transform>
</rapi:transforms>

But when I went though the module /v1/config/transforms/rest-tsm I am seeing an error response:

<error-response xmlns="http://marklogic.com/xdmp/error">
<status-code>406</status-code>
<status>Unacceptable Type</status>
<message-code>REST-UNACCEPTABLETYPE</message-code>
<message>
REST-UNACCEPTABLETYPE: (err:FOER0000) No acceptable content type: None of the requested types text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 can be provided
</message>
</error-response>

I can see the the module in Modules db. Which worked fine when I try to insert a document by using the transform.

Why can't I view the transform in the browser?


Solution

  • Unfortunately, that rest endpoint isn't very browser friendly. The required/acceptable Accept header values do not match what browsers will normally send.

    When you made the GET request through your browser, it was sending the following Accept header:
    text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

    Accept

    This field contains a semicolon-separated list of representation schemes ( Content-Type metainformation values) which will be accepted in the response to this request.

    Unfortunately, the v1/config/transform/{name} (GET) REST endpoint is strict in what it will accept for the Accept header and expects a specific value:

    The MIME type of the data expected in the response, either application/xslt+xml or application/xquery.

    If you use the example CURL command from the documentation, and customize for your transform URI, it will return the expected response.

    curl --anyauth --user public\admin:admin -X GET -i \
      -H "Accept: application/xquery" \
      http://localhost:9963/v1/config/transforms/rest-tsm