API Document upload example

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
Bastian
Posts: 7
Joined: Tue Dec 04, 2018 6:18 pm

API Document upload example

Post by Bastian » Wed Mar 13, 2019 4:06 pm

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

Bastian
Posts: 7
Joined: Tue Dec 04, 2018 6:18 pm

Re: API Document upload example

Post by Bastian » Thu Mar 14, 2019 10:07 am

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...

Post Reply