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?


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).

  • 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. Jul 30 '12 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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