efg
efg

Reputation: 187

Rename forest in MarkLogic 7 using rest-api extension

I'm able to rename forest using xquery in the console.
I converted xquery to rest and was able to install it as extension but when calling it using PUT request it doesn't work.
Curl will wait about 30 seconds then nothing and update is not made.
I'm not sure why that is. Any input is greatly appreciated. Here's my code:

$ cat ernest.xqy

  xquery version "1.0-ml";

   module namespace ernest =
       "http://marklogic.com/rest-api/resource/ernest";

   import module namespace admin = "http://marklogic.com/xdmp/admin"
       at "/MarkLogic/admin.xqy";


   declare variable $oldname := "old-forestname";
   declare variable $newname := "new-forestname";

   declare function ernest:put(
       $context as map:map,
       $params  as map:map,
       $input   as document-node()*
   ) as document-node()?
   {
     let $oldname := map:get($params, $oldname)
     let $newname := map:get($params, $newname)
     let $config := admin:get-configuration()
     let $config := admin:forest-rename($config, admin:forest-get-id(admin:get-configuration(), $oldname), $newname )
           return admin:save-configuration($config)
   };

Command to install it is:

curl --anyauth --user user:pass -X PUT -i -H "Content-type: application/xquery" -d@"./ernest.xqy" 'http://localhost:8040/v1/config/resources/ernest'

Result is:

HTTP/1.1 401 Unauthorized WWW-Authenticate: Digest realm="public", qop="auth", nonce="<some-hash>", opaque="<another-hash>" Content-type: text/html; charset=UTF-8 Server: MarkLogic Content-Length: 1942
   Connection: Keep-Alive Keep-Alive: timeout=5 HTTP/1.1 204 Updated Server: MarkLogic Content-Length: 0 Connection: Keep-Alive Keep-Alive: timeout=5

and I command to test it is:

curl --anyauth --user user:password -X PUT -H "Content-type: application/xquery" http://localhost:8040/v1/resources/ernest

I'm kind of lost at this point. I've learn a lot over the course of last couple of days but I think I'm hitting a wall here, so any suggestion would be greatly appreciated.

Thank you

Upvotes: 1

Views: 124

Answers (2)

Dave Cassel
Dave Cassel

Reputation: 8422

Step one: verify that your extension got deployed properly: in your browser, go to http://localhost:8040/v1/config/resources. The results should include at least the name, format, and supported methods.

Assuming that went well, I'd check that your parameters are getting seen in the extension code. Add some xdmp.log() statements to dump out your $oldname and $newname. If you aren't getting the values in your code, make sure you're using the rs: prefix on the parameters.

-- edit --

I think I figured it out. The key thing is that you weren't passing in the parameters when testing the extension. I added that, then figured out that curl expects to see some kind of a body when you use PUT, but that body will be the $input of your function, not the $params. $params is populated by the URL parameters. Here's the curl command that worked for me:

curl --anyauth --user admin:admin -X PUT -d 'undefined' \
  'http://localhost:8000/v1/resources/ernest?rs:old-forestname=docs&rs:new-forestname=Documents'

Note the -d 'undefined', as well as the rs: prefix on the parameters.

I used POSTman to figure this out -- it's a Chrome extension for sending HTTP requests. I got it working there, then it gave me the equivalent curl command.

Upvotes: 1

ehennum
ehennum

Reputation: 7335

Good that you've been making progress.

Because you are doing a PUT, curl might be expecting you to provide payload on the command line. Because you declare the content type to be application/xquery, it is in particular expecting an XQuery payload, which you don't need or want to send to this service.

While it's not strictly conformant to REST principles, you could cheat a little and change your resource service into a GET extension. Then you don't have to send a payload.

Hoping that helps,

Upvotes: 0

Related Questions