REST API Upload document to a cabinet

Anything concerning integration via the REST API
Post Reply
EdVonSchleck
Posts: 6
Joined: Tue Sep 17, 2019 7:33 am

REST API Upload document to a cabinet

Post by EdVonSchleck »

Sorry to ask again, this time as a new thread.
Maybe this time this question gets some attention.

I am trying to migrate a few thousend documents from another DMS to Mayan using the API.
All documents have tags and are organized in folders and sub-folders. The requirement ist to resemble the fodler structure in Mayan.
Luckily, there are cabinets. I can add cabinets to form a hierarchy.
However, I on my part had no luck so far getting documents in any of the cabinets.

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
User avatar
rosarior
Developer
Developer
Posts: 688
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: REST API Upload document to a cabinet

Post by rosarior »

I just tried it our and works as expected with version 3.3.12.

The result code should be 201.

it could be a permission issue, try it out on the UI with the same API user to validate it is correctly setup. The "Add documents to cabinets" permission must be granted for the cabinet and the document/document type.
2020-02-12_22-52.png
2020-02-12_22-52.png (44.66 KiB) Viewed 5352 times
2020-02-12_22-52_1.png
2020-02-12_22-52_1.png (87.98 KiB) Viewed 5352 times
EdVonSchleck
Posts: 6
Joined: Tue Sep 17, 2019 7:33 am

Re: REST API Upload document to a cabinet

Post by EdVonSchleck »

Thank you for your post. Sorry for the late reply. I have not had time to check your comments.

I checked the permissions of the import user. Just to recap what I did:
I created a role "importer" and added permission to add, see, and manipulate cabinets, to add/remove documents to cabinets.
I created a group "gImporter" and added the role "Importer" to it.
I created a user "import" and added the group "gImporter" to it.
I can login with user and manually create cabinet and add also add a document to it.
What exactly do you mean by permission for the docuemnt/document type? The type I am using is 'Default'.

Using the API I do in python:
res=requests.post(self.baseUrl+'/api/documents/',auth=(self.user,self.passwd),files={'file':f},data={'document_type':1})
docId=res.json()['id']
The document id is valid and the document exists in Mayan.

Then I do:
res=requests.post(self.baseUrl+'/api/cabinets/'+str(cabId)+'/documents/',auth=(self.user,self.passwd),data={'documents_pk_list':str(docId)})
I get the HTTP result 201 and the doc id in the response, so all seems to be fine on the API side.
However, the doc is not added to the cabinet.
Did you check on your side with uploading a real document and checking in the UI that the document is there?
EdVonSchleck
Posts: 6
Joined: Tue Sep 17, 2019 7:33 am

Re: REST API Upload document to a cabinet

Post by EdVonSchleck »

Is there a way to have a detailed debug log for API calls?
I have no idea how to fix my issue.
ilek
Posts: 5
Joined: Fri Feb 26, 2021 7:38 pm

Re: REST API Upload document to a cabinet

Post by ilek »

Just stumbled over the same problem when trying to add a document to a cabinet via the REST API.

In my case the solution was to slightly change the dictionary which is passed with the data parameter in the POST call.
Assuming that
- the document you want to add has document id 36
- the cabinet to which you want to add your document has cabinet id 9
- your Mayan 4.1.4 runs on http://127.0.0.1:8000

The URL to pass in the POST command would be http://127.0.0.1:8000/api/v4/cabinets/9/documents/add/ and the data parameter to pass in the POST request has to be data={'document': [36]}

So the trick is to have the key of the data dictionary passed in the POST call to be 'document' instead of 'documents_pk_id' and to pass the document id as a list in the value field of the dictionary. Got the idea that documents_pk_id might be an outdated key and the API expects a list of document ids when playing with the Swagger API cabinets_documents_add_create call gave me the response:

Code: Select all

{
  "document": [
    "This field is required."
  ]
}
Hope that helps - even if it comes with a delay of almost 2 years...
hamza
Posts: 1
Joined: Wed Mar 23, 2022 5:23 pm

Re: REST API Upload document to a cabinet

Post by hamza »

Same issue on running Mayan edms v 3.3.5
Posted document via rest api
then tagging documents to respected cabinet by providing valid cabinet id and document_pklist

issue :
Many of documents were tagging and uploading through same mechanism but upon volume all of this crashed suddenly.
Documents are now uploading to system but cannot be preview or for some the are left untagged.

Re- Tagging document via same mechanism return successful response from api but the still are not tagged and cannot be preview.
crafter
Posts: 4
Joined: Thu Sep 01, 2022 10:39 am

Re: REST API Upload document to a cabinet

Post by crafter »

The solution is not working for me.

I am using PHP to send create and upload a document, which works well.
Thereafter, trying to add the document to the cabinet results in the following error
{"detail":"Not found."}
I am at a loss on how to proceed as the API documentation is not clear about the request body and fails on the execute.

I would appreciate if someone who has overcome this and solved this and provide some guidance
I am using Version 4.2.5,

Code: Select all

            // Add document to cabinet
            $cabinet = $params['cabinet'];
            $data = array (
                'document' => [$document_id],   // values must be a list []
            );

            $post_data = json_encode($data);
            $request_url = 'http://mydomain.com/api/v4/cabinets/' . $cabinet . '/documents/add/';

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $request_url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);

            $headers = [];
            $headers[] = 'Accept: application/json';
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'Content-Length: ' . strlen($post_data);
            $headers[] = 'Authorization: Basic YWRtaW46QndLcFNaQnJURDI3MzN5';

            $result = curl_exec($ch);
            curl_close($ch);
User avatar
michael
Developer
Developer
Posts: 277
Joined: Sun Apr 19, 2020 6:21 am

Re: REST API Upload document to a cabinet

Post by michael »

Hello,

In version > 4.0 the API changed.

You have to pass a single document ID per request. This line is what is blocking your code from working:

Code: Select all

                'document' => [$document_id],   // values must be a list []
Cheers.
crafter
Posts: 4
Joined: Thu Sep 01, 2022 10:39 am

Re: REST API Upload document to a cabinet

Post by crafter »

michael wrote: Sat Sep 03, 2022 10:28 pm You have to pass a single document ID per request. This line is what is blocking your code from working:

Code: Select all

                'document' => [$document_id],   // values must be a list []
Thank you Micheal

I should have mentioned in my post that I tried various combinations of requests, including a single document. and even documents_pk_id as a single id or a list. without success.

I went back and tried the single document id just to be sure and doubled checked. However, I still get the same error '{"detail":"Not found."}'

I have confirmed that the cabinet id and document id exists on the mayan repository. I tried casting the parameter to int, string etc. Still no luck.
User avatar
michael
Developer
Developer
Posts: 277
Joined: Sun Apr 19, 2020 6:21 am

Re: REST API Upload document to a cabinet

Post by michael »

Try using a superadmin user to remove the possibility of a permission issue.
crafter wrote: Mon Sep 05, 2022 9:17 am
michael wrote: Sat Sep 03, 2022 10:28 pm You have to pass a single document ID per request. This line is what is blocking your code from working:

Code: Select all

                'document' => [$document_id],   // values must be a list []
Thank you Micheal

I should have mentioned in my post that I tried various combinations of requests, including a single document. and even documents_pk_id as a single id or a list. without success.

I went back and tried the single document id just to be sure and doubled checked. However, I still get the same error '{"detail":"Not found."}'

I have confirmed that the cabinet id and document id exists on the mayan repository. I tried casting the parameter to int, string etc. Still no luck.
Post Reply