[GIS] Add new style using REST api in geoserver

geoservergeoserver-rest-api

I have worked with geoserver rest api and it is great, The only problem is that I have no idea how to post content of my sld using api, I have this code

public void AddNewStyle(String styledata, string workspace)
        {
            String requestUrl = RestServiceUrl + "/workspaces/" + workspace + "/styles";

            String status = String.Empty;

            object payload = styledata;

            try
            {
                status = SendRestRequest(requestUrl, RequestMethod.Post, typeof(WorkspaceRequest), ref payload, ContentType.Html, AcceptType.Json);
            }



            catch (Exception ex)
            {
                throw new Exception("Adding datastores failed.", ex);
            }
        }

this code works fine with other requests. the only problem is I dont know what to send as styledata. I have set styledata as

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml"
  xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
  <NamedLayer>
    <Name>Baft Farsode</Name>
    <UserStyle>
      <Name>BaftFarsoode</Name>
      <Title>بافت فرسوده</Title>
      <Abstract>BaftFarsoode area</Abstract>
      <FeatureTypeStyle>
      <FeatureTypeName>Feature</FeatureTypeName>
        <Rule>
          <Title>بافت فرسوده</Title>
          <ogc:Filter>
            <ogc:PropertyIsEqualTo>
             <ogc:PropertyName>Farsoode</ogc:PropertyName>
             <ogc:Literal>1</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#4DFF4D</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>
         <Rule>
          <Title>بدون داده</Title>
          <ogc:Filter>

            <ogc:PropertyIsNull>
             <ogc:PropertyName>Farsoode</ogc:PropertyName>

            </ogc:PropertyIsNull>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#ffffff</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>

        <Rule>
          <Title>غیر فرسوده</Title>
          <ogc:Filter>
            <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>Farsoode</ogc:PropertyName>
                    <ogc:Literal>0</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#FF4D4D</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>

        <Rule>
          <Title>مرز املاک</Title>
          <LineSymbolizer>
            <Stroke>
              <CssParameter name="stroke-width">0.2</CssParameter>
            </Stroke>
          </LineSymbolizer>
        </Rule>
     </FeatureTypeStyle>
    </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

but it gives error, I think there must be another way to add style data as raw xml using rest api.

Can someone help me with this?

here is an example but it uploads sld file into geoserver. I want to send data as a raw string not by uploading.
http://docs.geoserver.org/latest/en/user/rest/examples/curl.html#creating-a-layer-style-sld-package

Best Answer

It's not clear what libraries you are using but as the example you link to shows you need to send the sld file (zipped and encoded) to http://localhost:8080/geoserver/rest/styles with the Content-type set.

I've never tried but I expect you could send just the XML data with a Content-type: text/xml but it will take longer than sending a compressed file.