我正在尝试使用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.
这不是一个允许的操作(如manual中所述)。任何更改工作空间名称的PUT
都会返回403错误。
您所能做的就是创建一个新工作区,复制旧工作区的内容然后将其删除。
根据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}。
上面提到了所有可能的情况(即200,404,405,500),并且没有像“无法更改工作空间的名称”这样的错误消息。在geoserver文件中!
你得到什么statusCode和statusMessage作为回应?你能确认你的{new_ws_sname}没有重复吗?