I have a function which publishes a layer in GeoServer with the REST API. Now I want to add an existing style in the layer while publishing it.

Anyone has any idea where I write the style name or call the 'style.sld' while publish the layer?

const publish = function (layerName) {
return new Promise((resolve, reject) => {
        method: 'POST',
        url: `http://${GEOSERVER_IP}:${GEOSERVER_PORT}/geoserver/rest/workspaces/workspaceName/datastores/datastoreName/featuretypes.xml`,
        auth: {
            username: `${GEOSERVER_ADMIN_USER}`,
            password: `${GEOSERVER_ADMIN_PASS}`
        headers: {
            'Content-Type': 'text/xml'
        data: `<featureType>
        .then(response => {
                ok: 1,
                message: `Layer has been published.`
        .catch(err => {
                ok: 0,
                message: err.response.data,


1 Answer 1


As the manual says you need to use POST the details to


To determine the format of the file I usually use a request like:

curl -u admin:geoserver http://localhost:8080/geoserver/rest/layers/topp:states/styles.xml

which returns this:

    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/pophatch.xml" type="application/atom+xml"/>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/polygon.xml" type="application/atom+xml"/>

So all you need to do is create a payload like:

    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/new_style.xml" type="application/atom+xml"/>


Alternatively, you could fetch and modify the layer.xml to add the new style information. So a request to the layer:

curl -u admin:geoserver http://localhost:8080/geoserver/rest/layers/topp:states.xml


    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/population.xml" type="application/xml"/>
  <styles class="linked-hash-set">
      <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/pophatch.xml" type="application/xml"/>
      <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/polygon.xml" type="application/xml"/>
  <resource class="featureType">
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/topp/datastores/states_shapefile/featuretypes/states.xml" type="application/xml"/>

So you would need to add a new defaultStyle or style block and then PUT it back to the layer endpoint.

  • 1
    Thank you. I actually can send style in Geoserver but Is it possible to publish a layer with that style(already in existing style list)? Or, we update the style of a newly created layer?
    – Angels
    Commented Aug 7, 2019 at 8:46
  • also I know the type of my style in .xml which is a line type.
    – Angels
    Commented Aug 7, 2019 at 8:52
  • I have described how you add an existing style to a layer.
    – Ian Turton
    Commented Aug 7, 2019 at 9:10

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.