【问题标题】:Geoserver Rest API update workspace nameGeoserver Rest API 更新工作区名称
【发布时间】:2017-02-18 03:29:04
【问题描述】:

我正在尝试使用 put 方法通过 geoserver rest api 更新工作区名称。

我收到“无法更改工作区的名称”。错误。

这是我的代码。

 $service = geoserver_url;
$data = "<workspace><name>testnew</name></workspace>";
        $url = $service . "rest/workspaces/workspacename";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $passwordStr = username:password
        curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"););
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');   
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);

        $info = curl_getinfo($ch);

    Any kind of help appreciated.

【问题讨论】:

    标签: php rest curl geoserver


    【解决方案1】:

    这是不允许的操作(如manual 中所述)。任何更改工作空间名称的 PUT 都会返回 403 错误。

    您所能做的就是创建一个新工作区,复制旧工作区的内容,然后将其删除。

    【讨论】:

      【解决方案2】:

      根据 geoserver REST API 文档 (link),您可以通过放置一个在其正文中包含 xml 字符串数据的请求来简单地编辑工作空间名称 (ws_name)。

      这里我给你举了一个例子。由于使用 Express 作为我的应用程序服务器,我使用 javascript 实现了一个请求,但它可以更改为您喜欢的语法。

      const options = {
        url: 'http://localhost:8080/geoserver/rest/workspaces/{current_ws_sname}',
        method: 'PUT',
        headers: { 'Content-type': 'text/xml' },
        body: '<workspace><name>{new_ws_name}</name></workspace>',
        auth: {
           user: {geoserver_username},
           pass: {geoserver_password}
        }
      

      为了根据上述选项变量执行请求,我在 Express 中使用了请求函数:

      request(options, (error, response, body) => {
        console.log('response status code:' , response.statusCode)
        console.log('response status message: ', response.statusMessage)
        const result = {}
        if (!error) {
           const statusCode = response.statusCode;
           const statusMessage = response.statusMessage;
           if (statusCode == 200) {
               result.err = false
               result.msg = `Modified`
           } else if (statusCode == 404) {
               result.err = true
               result.msg = 'Workspace not found!'
           } else if (statusCode == 405) {
               result.err = true
               result.msg = 'Forbidden to change the name of the workspace!'
                            //because of your username and/or password
           } else if (statusCode == 500) {
               result.err = true
               result.msg = 'Server internal error or duplicate names'
           }
        } else {
           result.err = true,
           result.msg = error;
        }
        res.send(result);
      })
      

      别忘了替换 {current_ws_sname}、{new_ws_sname}、{geoserver_username}、{geoserver_password} strong> 根据你自己的价值观。

      上面已经提到了所有可能的情况(即 200、404、405、500),并且在 geoserver 中没有像“无法更改工作空间的名称。”这样的错误消息文件!

      你得到什么 statusCode 和 statusMessage 作为响应?您能否确认您的 {new_ws_sname} 没有重复?

      【讨论】:

        猜你喜欢
        • 2013-04-19
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 2022-06-16
        • 2016-11-04
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多