DEV Community

Cover image for Update/Publish Styles on GeoServer from Javascript using REST
Ibrahim Awad
Ibrahim Awad

Posted on • Edited on

4

Update/Publish Styles on GeoServer from Javascript using REST

Hi there!
Do you want to publish or update some styles on GeoServer, that's easy, check out the official documentation for that. After you read it and can't use it in your javascript code, come here, this post will help you.

I'll skip the unnecessary chat about various situations you might need this and get right to it.
I'm using axios to handle my connections (because I like it :D), but you can do it with other libraries or even with vanilla JS.

Update an existing style (using PUT)

  • First prepare the StyleContent which is the content of the style file as if you normally would upload it through geoserver interface.
  • Make sure you have the right access, I'm using default username and password for geoserver admin and geoserver for this.
const geoserverURL = 'http://{GEOSERVER_HOST}/geoserver/rest/workspaces/{WORKSPACE}/styles/{STYLE_NAME}'
axios({
    method: 'put',
    url: geoserverURL,
    data: StyleContent,
    auth: {
        username: 'admin',
        password: 'geoserver'
    },
    headers: {'Content-Type': 'application/vnd.ogc.se+xml'}
}).then((response) => {}, (error) => {console.log(error);});
Enter fullscreen mode Exit fullscreen mode

Publish a new style

Guess what happens if you change the method from put to post from the previous code? That's correct! you publish a new style to geoserver.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free