How to delete a Geoserver layer using the Geoserver API with Python and Requests

How to delete a Geoserver Layer using the Geoserver API with Python and Requests.

We assume you have basic authentication on your Geoserver instance.

##  Python Code ###

import requests


def delete_layer(layer_name):

    s = requests.Session()
    GEOSERVER_USER = 'someusername'
GEOSERVER_PASS = 'asecretthingy'
s.auth = (GEOSERVER_USER, GEOSERVER_PASS)
    headers_json = {'Content-type': 'application/json', }
del_url
= "https:/yourgeoserver.com/workspaces/SsmeWorkspaceName/datastores/someDataStoreName/featuretypes/" + layer_name + "?recurse=true"
r = s.delete(del_url, headers=headers_json, data={})

  1. Create a requests session
  2. Enter your geoserver usernmame and password. A more secure way to do this would be to use dot env or simply have the environemental variables on the server already in place
  3. Pass in basic authentication to requests
  4. I choose the JSON api application content-type but Geoserver also support XML if thats your thing.
  5. Create your URL. Here the "?recurse=true" is needed as a layer could be in a group. A 403 will be returned if this is missing
  6. Make the DELETE request by passing in your URL and header data.


Comments

Popular Posts