Creating Layer in GeoServer using REST

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.


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.

Tags:

Rest

Geoserver