25

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.5k29 gold badges115 silver badges350 bronze badges
asked Aug 2, 2011 at 14:35
1
  • 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 Commented Jun 4, 2015 at 16:08

6 Answers 6

56

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
84.1k6 gold badges93 silver badges190 bronze badges
answered Jan 24, 2012 at 9:22
6
  • 2
    THANK YOU! Easy answer and just saved me another 2 hours of mocking around Commented Mar 3, 2012 at 0: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 Commented Mar 7, 2012 at 4:21
  • 1
    Wow - this is the best description of this I've seen. Commented Mar 4, 2017 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/ Commented Jun 14, 2017 at 3:25
  • Clear workflow description, relevant links included. UPVOTED! :) Commented Aug 25, 2018 at 10:40
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.

answered Mar 4, 2017 at 17:52
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.

answered Jan 24, 2012 at 9:08
1
  • 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 localhost:8080/geoserver/rest/workspaces/WORKSPACE_NAME/… -H "content-type: application/xml" -d "<featureType><name>AVAILABLE_FEATURETYPE_NAME</name></featureType>" Commented Jun 11, 2020 at 16:20
1

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

Ian Turton
84.1k6 gold badges93 silver badges190 bronze badges
answered Aug 2, 2011 at 14:52
2
  • 2
    Yeah, but there is no example for creating a feature type from a SQL statement... i was hoping someone had example code to share. Commented Aug 2, 2011 at 14:56
  • 4
    That user guide is severely lacking. Commented Mar 4, 2017 at 16:15
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>
answered Jun 4, 2015 at 16:32
1
  • 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: 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. Commented Mar 4, 2017 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"
 }
 }
}
answered Apr 6, 2021 at 20:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.