0

I am trying to upload a layer using the GeoServer (2.28) REST API using the Importer extension in python. I ak not not what I am doing wrong.If I use the PUT method when the sld does not exists in an empty workspace, it will load the layer and sld but will not apply the styling. If I reload a second time using the POST method for the same layer and sld in the same workspace the style is applied correctly. below are the code I am using, but I cannot figure out what I am doing wrong:

1.1 To upload a layer - (workspace is empty):

# Step 1: Create import job
POST /rest/imports
{
    "import": {
        "targetWorkspace": {
            "workspace": {
                "name": "city"
            }
        }
    }
}

# Step 2: Upload file to import task
POST /rest/imports/{import_id}/tasks
Files: towns.shp, towns.shx, towns.dbf (multipart form-data)

# Step 3: Execute import
POST /rest/imports/{import_id}
(no payload)

# Step 4: Execute task
POST /rest/imports/{import_id}/tasks/{task_id}
(no payload)

1.2 The SLD upload handling - (workspace is empty):

# Check if style exists
GET /rest/workspaces/city/styles/towns.json

# Since workspace is empty, style doesn't exist → CREATE NEW
POST /rest/workspaces/city/styles?raw=true&name=towns
Content-Type: application/vnd.ogc.sld+xml
[SLD XML content as raw bytes]

# Assign style to layer
PUT /rest/layers/city:towns
{
    "layer": {
        "defaultStyle": {
            "name": "towns"
        },
        "enabled": true
    }
}

2.1 To upload a layer - (workspace is not empty):

# Step 1: Create import job (SAME as Scenario 1)
POST /rest/imports
{
    "import": {
        "targetWorkspace": {
            "workspace": {
                "name": "city"
            }
        }
    }
}

# Step 2: Upload file to import task (SAME as Scenario 1)
POST /rest/imports/{import_id}/tasks
Files: towns.shp, towns.shx, towns.dbf (multipart form-data)

# Step 3: Execute import (SAME as Scenario 1)
POST /rest/imports/{import_id}
(no payload)

# Step 4: Execute task (SAME as Scenario 1)
POST /rest/imports/{import_id}/tasks/{task_id}
(no payload)

2.2 The SLD upload handling - (workspace is not empty)::

# Check if style exists
GET /rest/workspaces/city/styles/towns.json

# Since workspace already has styles, style likely EXISTS → UPDATE EXISTING
PUT /rest/workspaces/city/styles/towns.sld?raw=true
Content-Type: application/vnd.ogc.sld+xml
[SLD XML content as raw bytes]

# Assign style to layer (SAME as Scenario 1)
PUT /rest/layers/city:towns
{
    "layer": {
        "defaultStyle": {
            "name": "towns"
        },
        "enabled": true
    }
}

In code block 1.2; is application/vnd.ogc.sld+xml correct ?

1 Answer 1

0

To temporarily resolve the issue, I created a function that first creates the SLD, then immediately updates it—using the same SLD content. Since this is for internal use, I will continue with this approach until a more robust solution becomes available. The implementation reuses the same code provided in my original question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.