23

As part of a possible solution to GeoServer WFS Row Level Security? I want create a layer in GeoServer via REST that supplies a SQL statement along with a userid.

I think i would create a feature type and publish it. I need to do this all in REST.

Does anyone have any code examples?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
CaptDragon
  • 13,313
  • 6
  • 55
  • 96
  • Is looks like it is not possible to add a layer via rest interface, did you resolve it in the end? I get the error: HTTP Status 405 - The specified HTTP method is not allowed for the requested resource – a1an Jun 04 '15 at 16:08

6 Answers6

55

There are many things you can do with the GeoServer REST API that are not specifically documented and that there are not code examples for. Here's the strategy for tackling those.

First, start with the examples in the documentation. Make sure you are familiar with how you can create a simple new layer or workspace using an HTTP POST with either XML or JSON.

Then, through the GeoServer UI, manually create the object you need for which there is no documentation (in this case, a feature type).

Finally, manually browse to the GeoServer REST index (http://your-server/rest or http://your-server/geoserver/rest). Browse through the index until you find the feature type you just created. Append ".xml" or ".json" to the URL of this resource, and you will see its XML or JSON representation.

This representation is what you would have needed to POST to create the feature type through the API. The URL of the representation is the URL that you would have needed to POST to (for example, http://your-server/geoserver/rest/..../myFeatureTypeName.json).

You can use this strategy to figure out how to programmatically create or configure any resource in GeoServer.

Ian Turton
  • 81,417
  • 6
  • 84
  • 185
Rohan Singh
  • 896
  • 7
  • 6
  • 2
    THANK YOU! Easy answer and just saved me another 2 hours of mocking around – Ragi Yaser Burhum Mar 03 '12 at 00:24
  • 4
    In addition, a lot of the parameters can be skipped, and GeoServer will figure them out (just like in the UI). eg. bounding boxes – rcoup Mar 07 '12 at 04:21
  • 1
    Wow - this is the best description of this I've seen. – Kieveli Mar 04 '17 at 16:14
  • 6
    Using GeoServer 2.10.2, I found that I needed to POST to the featuretypes endpoint of the store, eg geoserver/rest/workspaces/<workspacename>/datastores/<storename>/featuretypes/ – Stephen Lead Jun 14 '17 at 03:25
  • Clear workflow description, relevant links included. UPVOTED! :) – tony gil Aug 25 '18 at 10:40
  • In addition to what @StephenLead is saying, I did have to POST to featuretypes (without final slash is ok), but I also had to set the header Content-type to application/json for it to accept my json body. Other than that - very clear explanation. – Grismar Sep 30 '19 at 03:49
18

I know this is an old question, but just in case anyone else is confused as I was. The important part is that you cannot create a layer from the http://geoserver/rest/layers endpoint.

If you are looking to add a vector layer, it is done through the feature type resource:

http://docs.geoserver.org/stable/en/user/rest/api/featuretypes.html

A POST to:

/workspaces/<ws>/datastores/<ds>/featuretypes

where is the workspace you want the feature type to live under, and the datasource to use will create a new vector feature type, ie layer.

If you are wondering about what you can POST, @Rohan is correct, the easiest way is to query for an existing feature type, IE an http GET to

/workspaces/<ws>/datastores/<ds>/featuretypes/<ft>.json 

which will return an existing feature type as json.

1

Your best reference is in the user guide. https://docs.geoserver.org/stable/en/user/rest/index.html#rest

Ian Turton
  • 81,417
  • 6
  • 84
  • 185
jdeolive
  • 1,757
  • 8
  • 9
1

If you understand Java you might want to take a look at this simple code that uses REST to do all kinds of operations on GeoServer.

Ivo Limmen
  • 133
  • 1
  • 5
  • 1
    This code (GsRest.java) helped me figure out a lot, like how to auto publish an 'available' featuretype from my postgis datastore. For reference it's like: curl -u admin:geoserver -v -XPOST http://localhost:8080/geoserver/rest/workspaces/WORKSPACE_NAME/datastores/DATASTORE_NAME/featuretypes -H "content-type: application/xml" -d "AVAILABLE_FEATURETYPE_NAME" – A. Mort Jun 11 '20 at 16:20
0

To add a layer, a datastore and a featuretype should be defined. Assuming they are already defined along with a style (as described in the guide at http://boundlessgeo.com/2012/10/adding-layers-to-geoserver-using-the-rest-api/ to add a layer a PUT request has to be sent to geoserver at:

http://geoserver.host/geoserver/rest/layers/NEW_LAYER_NAME

With a data content like:

<layer>
  <name>NEW_LAYER_NAME</name>
  <type>VECTOR</type>
  <defaultStyle>
    <name>myStyle</name>
  </defaultStyle>
  <resource class="featureType">
    <name>myFeature</name>
  </resource>
</layer>
a1an
  • 581
  • 1
  • 9
  • 20
  • That blog talks about adding a style to an existing layer, which makes sense as that is what PUT is designed for. You cannot create a new layer this way. Here is the docs: http://docs.geoserver.org/stable/en/user/rest/api/layers.html, a PUT to rest/layers will return a 405. A PUT to /rest/layers/<existing_layer> will modify a particular layer, not create a new one. – lostintranslation Mar 04 '17 at 17:54
0

Following the previous tips I was able to add a new layer

POST /geoserver/rest/workspaces/{workspace}/datastores/{datastore}/featuretypes
{
  "featureType": {
    "name": "my_layer_name"
  }
}

(my_layer_name is an existing resource on the datastore)

and to update the style

PUT /geoserver/rest/layers/{workspace}:my_layer_name
{
    "layer": {
        "name": "my_layer_name",
        "defaultStyle": {
            "name": "my_style"
        }
    }
}