File upload using REST API and Python requests

Thanks. Yes, I had had a look at that post request description in the docs. My question was basically about translating it into Python Requests.
Seems like I have figured out how to do it, so here’s an example of my working code in case somebody has the same question.

import requests
import json

API_KEY = 'XXXXXXXXXXXXXXXXXXX'  # my Omeka API key


def upload_file(order, item_id):
    url = 'http://my_omeka_instanse.org/api/files'
    params = {'key': API_KEY}  # packing Omeka key as payload

    # Building data structure for the request
    body_data = {
            "order": order,  # order of the file, integer
            "item": {"id": item_id},  # ID of the item to which the file should be attached
            "element_texts": [
                {
                    "html": False,
                    "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",  # Title of the document
                    "element": {"id": 50}  # ID of the element responsible for title (50 in Dublin Core)
                }
            ]
        }
    data = {'data': json.dumps(body_data)}  # packing body data to json and assigning name 'data' to it

    # Building info about my file, assigning name 'file'
    files = {
        'file':('my_file.doc',
                open('my_file.doc', 'rb'),
                'application/msword')
    }
    response = requests.post(url, params=params, data=data, files=files)
    print(response.content)


if __name__ == '__main__':
    upload_file(3, 11)
2 Likes