As the manual says you need to use POST the details to
/layers/<l>/styles[.<format>]
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:
<styles>
<style>
<name>pophatch</name>
<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"/>
</style>
<style>
<name>polygon</name>
<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"/>
</style>
</styles>
So all you need to do is create a payload like:
<styles>
<style>
<name>new_style</name>
<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"/>
</style>
<styles>
Update
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
gives:
<layer>
<name>states</name>
<path>/</path>
<type>VECTOR</type>
<defaultStyle>
<name>population</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/population.xml" type="application/xml"/>
</defaultStyle>
<styles class="linked-hash-set">
<style>
<name>pophatch</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/pophatch.xml" type="application/xml"/>
</style>
<style>
<name>polygon</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/polygon.xml" type="application/xml"/>
</style>
</styles>
<resource class="featureType">
<name>topp:states</name>
<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"/>
</resource>
<attribution>
<logoWidth>0</logoWidth>
<logoHeight>0</logoHeight>
</attribution>
</layer>
So you would need to add a new defaultStyle
or style
block and then PUT it back to the layer endpoint.