Last active
November 7, 2020 03:54
-
-
Save amoe/95bfb0bc8930a6d7f2572d24dd3d8679 to your computer and use it in GitHub Desktop.
Omeka-S post item with attached media using API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pprint | |
# Translated from https://forum.omeka.org/t/example-api-usage-using-curl/8083, | |
# the work of user 'kgoetz'. Thanks. | |
# Begin quote: | |
# > The API permits anonymous access to public resources (i.e., reading non-private | |
# > data). To perform actions or view data that only logged-in users can access, | |
# > you must authenticate your requests. | |
# > Authentication requires every request to include two additional GET | |
# > parameters: key_identity and key_credential. An Omeka S user can create API | |
# > keys in the "API keys" tab of their user edit page. | |
install_location = "http://omeka-s.cslp019129.hist.susx.ac.uk" | |
key_identity = 'REDACTED' | |
key_credential = 'REDACTED' | |
endpoint = "{}/api/items?key_identity={}&key_credential={}" | |
postdata = {} | |
final_uri = endpoint.format(install_location, key_identity, key_credential) | |
predefined_item_set_id = 5 | |
item_payload = { | |
"dcterms:title": [ | |
{ | |
"property_id": 1, | |
"property_label": "Title", | |
"@value": "My snappy title API upload style", | |
"type" : "literal" | |
} | |
], | |
"@type": "o:Item", | |
"o:item_set": [ | |
{"o:id": predefined_item_set_id} | |
], | |
"o:media" : [ | |
{"o:ingester": "upload", "file_index": "0", "o:item": {}, | |
"dcterms:title": [ | |
{ | |
"property_id": 1, | |
"property_label": "Title", | |
"@value" : "My media upload title", | |
"type" : "literal" | |
} | |
] | |
} | |
] | |
} | |
r = requests.post(final_uri, data={'data': json.dumps(item_payload)}, files=[ | |
('file[0]', ('example.png', open('example.png', 'rb'), 'image/png')) | |
]) | |
if r.ok: | |
pprint.pprint(r.json()) | |
print(r.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment