03-26-2021 04:41 AM
I am trying to upload a file using python requests lib.
I have gotten the file upload working. using
requests=(files={'filedata': open(file)})
But how can I add properties to the file to upload?
I have some links to possible solutions, but I have not gotten any of them to work.
1) https://stackoverflow.com/questions/32585800/convert-curl-request-to-python-requests-request
2) https://stackoverflow.com/questions/66796487/alfresco-basic-authentication-works-with-curl-but-not-w... (my post)
Content-Length: 248588 Content-Type: multipart/form-data; boundary=2eda0224894a7955aa73990f6d06b87e 2eda0224894a7955aa73990f6d06b87e Content-Disposition: form-data; name="properties" cm:title --2eda0224894a7955aa73990f6d06b87e Content-Disposition: form-data; name="properties" cm:description --2eda0224894a7955aa73990f6d06b87e Content-Disposition: form-data; name="properties" cm:author %PDF-1.4 %Çì¢ 5 0 obj <</Length 6 0 R/Filter /FlateDecode>> stream
03-26-2021 06:14 AM
Content-Length: 248595 Content-Type: multipart/form-data; boundary=b4407dae6a30a4e93ba1c0e857cce06b --b4407dae6a30a4e93ba1c0e857cce06b Content-Disposition: form-data; name="data" Content-Type: application/json {"properties": {"cm:title": "info", "cm:description": "info", "cm:author": info}} --b4407dae6a30a4e93ba1c0e857cce06b Content-Disposition: form-data; name="filedata"; filename="application_of_dependency_graphs_to_security_protocol_analysis_0.1_T-5-4.pdf" %PDF-1.4 %Çì¢ 5 0 obj
I got it into a better state. But it still doesn't read the fields. The problem is things like parameter names. Alfresco Backend reads the file only with parameter 'filedata'. So what is the parameter 'data' or 'json' supposed to be? Is the structure wrong there?
03-26-2021 07:39 AM
I have a feeling you can't add the title/description/authors metadata in post request.
I made a second PUT request right after a successful POST request ( you get the inserted object ID back with POST)
and i was able to add the props successfully
{"properties":{"cm:description":"blaah" etc. }
03-26-2021 12:55 PM
Hi @Taaviansper
I've just tested using the API to upload a document with metadata successfully.
I've used Postman to test the API and it will generate code - here is the Python code:
import requests url = "http://localhost/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?alf_ticket=TICKET_1d41b867df221a4d0edcd161d4df7570f1a07f98" payload={'name': 'Marketing Automation.ppt', 'autoRename': 'true', 'cm:title': 'Marketing Automation', 'cm:description': 'A powerpoint about marketing and automation', 'exif:manufacturer': 'Canon', 'cm:author': 'Eddie May'} files=[ ('filedata',('Marketing_Automations_Trial.pptx',open('/Users/emay/Downloads/Marketing_Automations_Trial.pptx','rb'),'application/vnd.openxmlformats-officedocument.presentationml.presentation')) ] headers = { 'Authorization': 'Basic dGVzdDp0ZXN0' } response = requests.request("POST", url, headers=headers, data=payload, files=files) print(response.text)
Note that in the payload I've set properties such as title & description. You can also see that it's a POST request.
The result in share is as follows:
You can see from the creation date that I did it today (26 March 2021).
This should work from Alfresco 5.2 upwards. Here I'm testing against Community Edition 6.2.
HTH,
Explore our Alfresco products with the links below. Use labels to filter content by product module.