Looking for some example code for uploading a file using the API

Hello, I am looking at the docs for uploading a file (PDF) using the API /api/v4/documents/upload/ (documents_upload_create)

Does anyone have a code example of how they have done it? I am unclear how to upload the binary of the file?

I assume this is the correct endpoint?

Thanks for all help
Randy

I don’t know about the ‘upload’ method. I personally use the new document method by directly using POST on documents. That was the way in 3.X

Since v4 that changed, using that is a two step process. First you need to create the doc, then upload the file.

Creating the doc gives you an ID.

Then you need to upload the file.

It the documentation you can see this process here:

https://docs.mayan-edms.com/chapters/rest_api.html#examples

Here is the file/binary part:

with open(file=‘test_document.pdf’, mode=‘rb’) as file_object:
response = requests.post(url=‘http://127.0.0.1:8000/api/v4/documents/1/files/’, auth=(‘username’, ‘password’), files={‘file_new’: file_object}, data={‘action’: 1})

response.status_code
202

Thank you for the help!

Did it work? I was about to respond. The “1” in action is an actual “one”, not an ID. The “1” in the URL is the document ID.

talbottech, thank you for all your help.
With your suggestions i was able to make it work (after fighting with CORS forever)
Thanks

i was able to make it work (after fighting with CORS forever)

How did you solved it? Share sample code or settings?

I ended up not using the nginx server. (since cors was already enabled on mayan there were conflicts) the the server hosting the docker image did not have port 80 or 443 open, once i opened those it worked.

I did have to manually edit
vi ./mayan-edms/lib/python3.9/site-packages/corsheaders/middleware.py
and add

        response[ACCESS_CONTROL_ALLOW_HEADERS] = ",X-Session-Token, ".join(conf.CORS_ALLOW_HEADERS)

for my installation. I do need to figure out how to do this edit better , kind of a hack right now but it works.