Skip to content

Instantly share code, notes, and snippets.

@pbinkley
Created September 3, 2020 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbinkley/d6f34afe7d1972347ddb51832a6fca1d to your computer and use it in GitHub Desktop.
Save pbinkley/d6f34afe7d1972347ddb51832a6fca1d to your computer and use it in GitHub Desktop.
Omeka-S REST API: minimal working example of creating an item with uploaded image in Ruby
=begin
This is a working example of the use of the Omeka-S REST API to create an item
with an associated image. It assumes you've set the API endpoint (e.g.
http://localhost:8080/api) and the authentication keys as environment
variables.
Note that the o:media element is an array, and can contain multiple images
(with sequential 'file_index' values, which must match the 'file[x]' element
in the payload).
=end
require 'json'
require 'rest-client'
item = {
'o:resource_class': { 'o:id': 26 }, # optional
'o:resource_template': { 'o:id': 2 }, # optional
'o:item_set': [{ 'o:id': 2 }], # optional
'o:media': [
{
'o:ingester': 'upload',
'file_index': '0',
'o:item': {},
'dcterms:title': [
{
'property_id': 1,
'property_label': 'Title',
'@value': 'My Image',
'type': 'literal'
}
]
}
],
'dcterms:title': [
{
'type': 'literal',
'property_id': 1,
'property_label': 'Title',
'is_public': true,
'@value': 'My Item'
}
]
}
payload = {
'data' => item.to_json,
'file[0]' => File.new('./images/my_image.jpg', 'rb')
}
response = RestClient.post ENV['OMEKA_API'] + '/items',
payload,
params: {
key_identity: ENV['OMEKA_KEY_IDENTITY'],
key_credential: ENV['OMEKA_KEY_CREDENTIAL']
}
puts "Result: #{response.code}"
if response.code == 200
response_body = JSON.parse(response.to_s)
puts "New item: #{response_body['@id']}"
puts "New media: #{response_body['o:media'][0]['@id']}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment