Issue when adding media with metadata via API

Hello,

I’m trying to add a media file (a picture.jpg) to an existing item using the Omeka S API. The media itself is successfully added to the item, which is great. However, I’ve been struggling for two days to include metadata (like title, creator, description, etc.) for the media, and nothing seems to work — the metadata is never saved.

Here is the code I’m using (python):

import requests
import json

url = “https://omekas.test/api/media?key_identity=YDJvt5PMD1VaqB87dZ7O8C21pUsDoDPR&key_credential=BWOWBuMRkaWLlFfKoKqbc01HWrLYHJhB
files = {
“file[0]”: (“B987352101_FCG21-3_001.jpg”, open(“jpg/B987352101_FCG21-3_001.jpg”, “rb”), “image/jpeg”)
}
data = {
“data”: json.dumps({
“o:ingester”: “upload”,
“file_index”: “0”,
“o:item”: {“o:id”: 54728},
“dcterms:title”: [{“@value”: “Titre ajoute a la creation”, “type”: “literal”}],
“dcterms:creator”: [{“@value”: “Jean-Claude Bosmel”, “type”: “literal”}],
“dcterms:spatial”: [{“@value”: “Moorea”, “type”: “literal”}],
“dcterms:description”: [{“@value”: “Description ajoutee a la creation”, “type”: “literal”}]
})
}

response = requests.post(url, files=files, data=data)
if response.status_code == 200:
print(“-- Média créé avec succès.”)
print(json.dumps(response.json(), indent=2))
else:
print(f"-- Erreur API : {response.status_code}")
print(response.text)

Could you please let me know what I might be doing wrong? Is there something I’m missing about how metadata should be structured when creating a media?

Thank you very much for your help and for all the work on Omeka S!

Best regards,

Marc

The API is pretty picky about the format it expects for metadata values, and in your example here you’re missing something that’s required: every value needs a property_id.

If this is an up-to-date Omeka S (4.0.0 or newer), you can put auto as the property_id for every value, but it does need to be there, e.g.:

"dcterms:title": [{"@value": "Titre ajoute a la creation", "type": "literal", "property_id": "auto"}]

Thank you for your help — it worked perfectly!

I just wanted to sincerely thank you for your help regarding the metadata format. Adding "property_id": "auto" did the trick — the metadata is now being saved correctly along with the media.

Thanks to your guidance, I’ll now be able to process and enrich around 1,200 photos with metadata coming from an Excel file. This will be a huge time saver and a major step forward for our project.

Really appreciate your responsiveness and the quality of your support.

Warm regards,

Marc