After uploading data to GeoServer and displaying it in GeoNode, I want to to update specific parts of the metadata using the REST API. My goal is to update the groups
value, which is empty (NULL
) after uploading the data. I use a GET
first to retrieve the metadata and then enter the new value for group
. But when I try to put
or patch
the metadata I receive Failed to update group. Status code: 500
. This is my function to adjust the metadata:
def update_layer_metadata(layer_pk, group_value):
# Endpoint for the specific layer
url = f"{GEONODE_API_URL}/{layer_pk}/"
# Fetch existing metadata
response = requests.get(url, auth=(GEONODE_USERNAME, GEONODE_PASSWORD))
if response.status_code != 200:
print(f"Failed to fetch existing metadata. Status code: {response.status_code}")
return
# Parse the metadata and update the 'group' field
metadata = response.json()
if "dataset" in metadata and "group" in metadata["dataset"]:
metadata["dataset"]["group"] = group_value
else:
print("Key 'group' not found in metadata or invalid metadata format.")
return
# Send updated metadata
update_response = requests.put(
url,
json=metadata,
# headers={"Content-Type": "application/json"},
auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
)
if update_response.status_code in [200, 204]:
print("Group updated successfully.")
else:
print(f"Failed to update group. Status code: {update_response.status_code}")
According to the Django REST framework Site, which I access by using the value of url
, the allowed operations should be GET
, PATCH
and PUT
.
Update:
I read that the URL should end on "/metadata/"
, so I adjusted the variable url
and this resulted in a new error code: Failed to update group. Status code: 405
. This was the adjusted part:
# Send updated metadata
update_response = requests.patch(
url+"metadata/",
json=metadata, # Sending only the required field
# headers={"Content-Type": "application/json"},
auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
)