Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Following information at this link in the section on creation of layer groups, I've created the method(C#):

public bool AddToLayerGroup(string layerGroupName, string layerName)
                string gUrl = GEOSERVER_HOST + "/rest/layergroups";
                WebRequest request = WebRequest.Create(gUrl);

                request.ContentType = "application/xml";
                request.Method = "POST";
                request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);

                byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(GetAddToLayerGroupXml(layerGroupName, layerName));
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);

                WebResponse response = request.GetResponse();

                return true;
            catch (Exception)
                return false;

When I run a unit test against this method, I get an Internal Server Error reported from GeoServer if I use POST HTTP method.

Now the layerGroupName and layerName used in the above method are already existing in GeoServer. What I want to achieve is provide a way to create a layer group if it doesn't exist, or just use an existing one. The layerName always already exists in GeoServer, as I'm using a separate method to create them.

Can anyone spot any problem with my code above. Or, suggest an alternative approach. cURL or Java or C# examples are welcome. Thanks in advance.

UPDATE: From Tomcat logs, GeoServer complains that the layer group I've specified already exists. Is there away to add a layer to an existing layer group using the REST API?

share|improve this question
up vote 3 down vote accepted

A POST will always create a new resource and redirect you to it once created. If you want to either create a new one or replace an existing one you must use PUT instead. Mind, it will overwrite fully an existing layer group (btw, haven't tried it out, the above is just based on what you should expect from a REST based API).

share|improve this answer
Thanks, @Andrea, for the swift response. I don't wish to replace an existing layer group; I just want to reuse it. Is there away to reuse a layer group without affecting the layers it already has? – okello Jul 30 '12 at 12:13
You have to get its representation using GET, add the new layer into it, and then PUT it back. You cannot modify it. – Andrea Aime Jul 30 '12 at 17:11
Thanks. This was very helpful. – okello Jul 30 '12 at 17:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.