使用 rest-api 扩展重命名 MarkLogic 7 中的森林

Rename forest in MarkLogic 7 using rest-api extension

我可以在控制台中使用 xquery 重命名林。
我将 xquery 转换为 rest 并能够将其作为扩展安装,但是当使用 PUT 请求调用它时它不起作用。
Curl 将等待大约 30 秒,然后什么也没有,也不会进行更新。
我不确定为什么会这样。非常感谢任何输入。 这是我的代码:

$猫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)
   };

安装它的命令是:

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

结果是:

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

我测试它的命令是:

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

此时我有点迷路了。在过去的几天里我学到了很多东西,但我想我在这里碰壁了,所以任何建议将不胜感激。

谢谢

很高兴你一直在进步。

因为您正在执行 PUT,curl 可能希望您在命令行上提供负载。因为您将内容类型声明为 application/xquery,所以它特别需要 XQuery 负载,您不需要或不想将其发送到此服务。

虽然它不严格符合 REST 原则,但您可以稍微作弊,将您的资源服务更改为 GET 扩展。那么你不必发送有效负载。

希望对您有所帮助,

第一步:验证您的扩展是否已正确部署:在您的浏览器中,转到 http://localhost:8040/v1/config/resources。结果应至少包括名称、格式和支持的方法。

假设一切顺利,我会检查您的参数是否在扩展代码中可见。添加一些 xdmp.log() 语句来转储您的 $oldname$newname。如果您没有获得代码中的值,make sure you're using the rs: prefix on the parameters

-- 编辑 --

我想我明白了。关键是您在测试扩展时没有传入参数。我补充说,然后发现当您使用 PUT 时,curl 期望看到某种主体,但该主体将是您函数的 $input,而不是 $params。 $params 由 URL 参数填充。这是对我有用的 curl 命令:

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

注意 -d 'undefined' 以及 rs: prefix on the parameters

我使用 POSTman 解决了这个问题——它是一个用于发送 HTTP 请求的 Chrome 扩展。我让它在那里工作,然后它给了我等效的 curl 命令。