Page 1 of 1

API Document upload example

Posted: Wed Mar 13, 2019 4:06 pm
by Bastian
Hi @all

I'm trying to upload a file with a python script and requests library to a cabinet.

So far I've managed to fetch the correct cabinet id and to create a new cabinet if it doesn't exist already.

But how do I upload a document (pdf) into that cabinet?

Does anyone have a codesnippet?

here's what I did so far - but with no success...

I always get status_code 400 with message

{"documents_pk_list":["This field is required."]}

Code: Select all

import requests

doctype = 2 
baseurl = 'http://172.21.0.100:8000/api'
cred = ('admin', 'password')

demo_cabin_id = 5
fnam = 'AN1903-001.pdf'

payload = {'description': 'Test Dokument',
           'document_type': doctype, 'label': fnam, 'language': 'Deutsch'}
files = {'file': open(fnam, 'rb')} 

# Upload URL
url = f'{baseurl}/cabinets/{demo_cabin_id }/documents/'

r = requests.post(url, files=files, data=payload, auth=cred)
print(r.status_code)
print(r.text)

Any help is greatly appreciated

regards,

Bastian

Re: API Document upload example

Posted: Thu Mar 14, 2019 10:07 am
by Bastian
ok,

I've got the file uploaded.

I used the wrong base url
it needs to be uploaded to /api/documents

than it works.

Now I need to add the uploaded document to a cabinet.

We will see...

Re: API Document upload example

Posted: Fri Dec 20, 2019 10:59 am
by EdVonSchleck
Hi Bastian,

did you get the last step to work - adding a document to a cabinet?

I on my part had no luck so far. I tried python:

Code: Select all

res=this.Session.post(this.Url+"/cabinets/"+str(parentId)+"/documents/",data={"documents_pk_list":str(docId)}).json()
where parentId is a valid id of a cabinet and docId is the id returned when adding it to mayan.
However, the function returns ok but no document is added

Alternatively, I tried curl:

Code: Select all

curl -u admin:pass -X POST "http://127.0.0.1:8000/api/cabinets/11/documents/" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"documents_pk_list\":\"116\"}"
No response but also no document in the cabinet.

Any hints?

Thank you,
Ed