Creating Workspace, Data Store and Layer via GeoServer REST API?

I use node/request;

to add a workspace

curl -u admin:geoserver -v -XPOST -H Content-Type:application/xml -d @test.xml http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-Type:application/json -d @test.json http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-type:text/xml -d "myworkspace1" http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-type:text/json -d "{workspace:{name:'string'}}" http://192.168.1.254:8083/geoserver/rest/workspaces

use ndoejs do this:

//create a workspace
request.post('http://192.168.1.254:8083/geoserver/rest/workspaces',{
headers:{
    'Content-Type':"application/json"
},
body:JSON.stringify({
    "workspace":{
        "name":"test"
    }
})
},function(error,response,body){
if(error){
    console.log(error)
};
console.log(response.statusCode);//201 created ok or 401 has alreay one;
console.log(body);
}).auth('admin','geoserver');

to add a datastore:

request.post('http://192.168.1.254:8083/geoserver/rest/workspaces/test/datastores',{
headers:{
    'Content-Type':"application/json"
},
body:JSON.stringify({
    "dataStore":{
        "name":"buildings",
        "connectionParameters": {
            "entry": [
              {"@key":"host","$":"192.168.1.254"},
              {"@key":"port","$":"5432"},
              {"@key":"database","$":"$$$"},
              {"@key":"user","$":"$$$$"},
              {"@key":"passwd","$":"gss7"},
              {"@key":"dbtype","$":"postgis"}
            ]
        }
    }
})
},function(error,response,body){
if(error){
            console.log(error)
        };
        console.log(response.statusCode);//201 created ok or 404 has alreay 
  deleted;
        console.log(body);
 }).auth('admin','geoserver');

add a layer is somehow confused ,it said

To create a new layer, instead POST to one of /workspaces/{workspaceName}/coveragestores/{coveragestoreName}/coverages, /workspaces/{workspaceName}/datastores/{datastoreName}/featuretypes, /workspaces/{workspaceName}/wmsstores/{wmsstoreName}/wmslayers, or /workspaces/{workspaceName}/wmtsstores/{wmststoreName}/wmtslayers

I'll complete this after I success.


Here's a workaround using Selenium, which opens a web browser and programatically clicks the 4 checkboxes:

from selenium import webdriver

serverUrl = "http://<my_geoserver>:8080/geoserver/web/"
workspaceName = "<workspaceName>"
login = "admin"
pwd = "geoserver"

# Open a browser and log in to the GeoServer admin page
browser = webdriver.Chrome()
browser.get(serverUrl)
browser.find_element_by_id("username").send_keys(login)
browser.find_element_by_id("password").send_keys(pwd)
browser.find_element_by_class_name("positive").click()

# Navigate to the new Workspace, check the Services checkboxes, and Save
browser.get(serverUrl + "bookmarkable/org.geoserver.web.data.workspace.WorkspaceEditPage?name=" + workspaceName)
for idx in range (0,4):
  chkBox =  browser.find_element_by_name("services:services:" + str(idx) + ":enabled")
  if not (chkBox.is_selected()):
    chkBox.click()
browser.find_element_by_id("id9").click()

It's a little hacky, and may require editing if the GeoServer UI elements change (eg login = .positive, save = #id9) but is good enough to allow me to automate this process.