1

I am using REST APIs to publish and change the default style of layers published in GeoServer. When I copy the SLD and paste it directly in the GeoServer styles it works fine. but while using REST API it omits the complete symbology and no style is assigned. imported symbology is as follows

<sld:PolygonSymbolizer>
            <sld:Fill/>
            <sld:Stroke/>
          </sld:PolygonSymbolizer>

following script loads sld from local file and publishes it using REST API:

async function loadSld(urls) {
  try {
    const response = await fetch(urls);
    sld = await response.text();
  } catch (err) {
    console.error(err, 'error loading sld');
  }

  const url = "http://localhost:8080/geoserver/rest/styles";
    await fetch(url, {
      method: "POST",
      headers: {
        "Content-type": "application/vnd.ogc.sld+xml",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
        Authorization: "Basic YWRtaW46Z2Vvc2VydmVy",
      },
      body: sld,
    })
      .then((response) => {
        res = response;
        console.log(res, 'response after sld');
        console.log(sld, 'sld');
        if (res.statusText !== "Created") {
         console.log(res, 'response');
        }
      })
      .catch((error) => {
        console.error("Error:", error);
      });
}

the sld is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1.0" xmlns:se="http://www.opengis.net/se">
  <NamedLayer>
    <se:Name>Pakistan_with_Kashmir</se:Name>
    <UserStyle>
      <se:Name>Pakistan_with_Kashmir</se:Name>
      <se:FeatureTypeStyle>
        <se:Rule>
          <se:Name>0 - 1.6</se:Name>
          <se:Description>
            <se:Title>0 - 1.6</se:Title>
          </se:Description>
          <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
            <ogc:And>
              <ogc:PropertyIsGreaterThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>0</ogc:Literal>
              </ogc:PropertyIsGreaterThanOrEqualTo>
              <ogc:PropertyIsLessThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>1.60000000000000009</ogc:Literal>
              </ogc:PropertyIsLessThanOrEqualTo>
            </ogc:And>
          </ogc:Filter>
          <se:PolygonSymbolizer>
            <se:Fill>
              <se:SvgParameter name="fill">#ffffff</se:SvgParameter>
            </se:Fill>
            <se:Stroke>
              <se:SvgParameter name="stroke">#232323</se:SvgParameter>
              <se:SvgParameter name="stroke-width">1</se:SvgParameter>
              <se:SvgParameter name="stroke-linejoin">bevel</se:SvgParameter>
            </se:Stroke>
          </se:PolygonSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>1.6 - 3.2</se:Name>
          <se:Description>
            <se:Title>1.6 - 3.2</se:Title>
          </se:Description>
          <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
            <ogc:And>
              <ogc:PropertyIsGreaterThan>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>1.60000000000000009</ogc:Literal>
              </ogc:PropertyIsGreaterThan>
              <ogc:PropertyIsLessThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>3.20000000000000018</ogc:Literal>
              </ogc:PropertyIsLessThanOrEqualTo>
            </ogc:And>
          </ogc:Filter>
          <se:PolygonSymbolizer>
            <se:Fill>
              <se:SvgParameter name="fill">#ffbfbf</se:SvgParameter>
            </se:Fill>
            <se:Stroke>
              <se:SvgParameter name="stroke">#232323</se:SvgParameter>
              <se:SvgParameter name="stroke-width">1</se:SvgParameter>
              <se:SvgParameter name="stroke-linejoin">bevel</se:SvgParameter>
            </se:Stroke>
          </se:PolygonSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>3.2 - 4.8</se:Name>
          <se:Description>
            <se:Title>3.2 - 4.8</se:Title>
          </se:Description>
          <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
            <ogc:And>
              <ogc:PropertyIsGreaterThan>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>3.20000000000000018</ogc:Literal>
              </ogc:PropertyIsGreaterThan>
              <ogc:PropertyIsLessThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>4.79999999999999982</ogc:Literal>
              </ogc:PropertyIsLessThanOrEqualTo>
            </ogc:And>
          </ogc:Filter>
          <se:PolygonSymbolizer>
            <se:Fill>
              <se:SvgParameter name="fill">#ff8080</se:SvgParameter>
            </se:Fill>
            <se:Stroke>
              <se:SvgParameter name="stroke">#232323</se:SvgParameter>
              <se:SvgParameter name="stroke-width">1</se:SvgParameter>
              <se:SvgParameter name="stroke-linejoin">bevel</se:SvgParameter>
            </se:Stroke>
          </se:PolygonSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>4.8 - 6.4</se:Name>
          <se:Description>
            <se:Title>4.8 - 6.4</se:Title>
          </se:Description>
          <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
            <ogc:And>
              <ogc:PropertyIsGreaterThan>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>4.79999999999999982</ogc:Literal>
              </ogc:PropertyIsGreaterThan>
              <ogc:PropertyIsLessThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>6.40000000000000036</ogc:Literal>
              </ogc:PropertyIsLessThanOrEqualTo>
            </ogc:And>
          </ogc:Filter>
          <se:PolygonSymbolizer>
            <se:Fill>
              <se:SvgParameter name="fill">#ff4040</se:SvgParameter>
            </se:Fill>
            <se:Stroke>
              <se:SvgParameter name="stroke">#232323</se:SvgParameter>
              <se:SvgParameter name="stroke-width">1</se:SvgParameter>
              <se:SvgParameter name="stroke-linejoin">bevel</se:SvgParameter>
            </se:Stroke>
          </se:PolygonSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>6.4 - 8</se:Name>
          <se:Description>
            <se:Title>6.4 - 8</se:Title>
          </se:Description>
          <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
            <ogc:And>
              <ogc:PropertyIsGreaterThan>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>6.40000000000000036</ogc:Literal>
              </ogc:PropertyIsGreaterThan>
              <ogc:PropertyIsLessThanOrEqualTo>
                <ogc:PropertyName>PROVINCE_C</ogc:PropertyName>
                <ogc:Literal>8</ogc:Literal>
              </ogc:PropertyIsLessThanOrEqualTo>
            </ogc:And>
          </ogc:Filter>
          <se:PolygonSymbolizer>
            <se:Fill>
              <se:SvgParameter name="fill">#ff0000</se:SvgParameter>
            </se:Fill>
            <se:Stroke>
              <se:SvgParameter name="stroke">#232323</se:SvgParameter>
              <se:SvgParameter name="stroke-width">1</se:SvgParameter>
              <se:SvgParameter name="stroke-linejoin">bevel</se:SvgParameter>
            </se:Stroke>
          </se:PolygonSymbolizer>
        </se:Rule>
      </se:FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

2 Answers 2

2

You are posting a SLD 1.1 to GeoServer, but telling it, via the mime type, it's a SLD 1.0 instead.

Try using the SLD 1.1 mime type instead: application/vnd.ogc.se+xml

0

I'm using SLD 1.0, you can try this:

Create style name

curl -u admin:pass -X POST 'https://site.com/geoserver/rest/styles' \
    --header 'Content-type: text/xml' \
    --data-raw "<style><name>style_name</name><filename>sld_name.sld</filename></style>"

Upload style code in new style

curl -u admin:pass -X PUT "https://site.com/geoserver/rest/styles/style_name" \
    --header 'Content-type: application/vnd.ogc.sld+xml' \
    --data-binary "@local/path/to/sld_name.sld"

You can use "Authorization Basic" instead of admin:pass

curl -X PUT "https://site.com/geoserver/rest/styles/style_name" \
    --header 'Content-type: application/vnd.ogc.sld+xml' \
    --header 'Authorization: Basic Y***************='
    --data-binary "@local/path/to/sld_name.sld"

If use self-signed certificate for https, use -k option

curl -k -X PUT ****

Your Answer

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

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