pythoncurluploadalfresco

Uploading file with Python and Alfresco API


I'm facing a problem with Alfresco and Python : I want to upload a new file directly with Python. Each time I try I get a HTTP 500 error with no details ...

I first try with CURL just to be sure that it works and the file is uploaded without problems.

I used the following curl command line:

url -X POST -uadmin:admin "http://localhost:8080/alfresco/service/api/upload" -F filedata=@/tmp/eeeeee.pdf -F siteid=test -F containerid=documentLibrary

In my Python script I tried PyCurl and now the simple urllib. I think the problem come from the way the file is given as parameter.

Python code using PyCurl:

c = pycurl.Curl()
params = {'containerid': 'documentLibrary', 'siteid': 'test', 'filedata': open('/tmp/eeeeee.pdf', 'rb')}
c.setopt(c.URL, 'http://localhost:8080/alfresco/service/api/upload')
c.setopt(c.POST, 1)
c.setopt(c.POSTFIELDS, urllib.urlencode(params))
c.setopt(c.USERPWD, 'admin:admin')
c.perform()

This code sample leads to :

 {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  },  

  "message" : "04200017 Unexpected error occurred during upload of new content."

Does someone know how to achieve that?


Solution

  • You can use the very simple library Requests.

    import json
    import requests
    
    url = "http://localhost:8080/alfresco/service/api/upload"
    auth = ("admin", "admin")
    files = {"filedata": open("/tmp/foo.txt", "rb")}
    data = {"siteid": "test", "containerid": "documentLibrary"}
    r = requests.post(url, files=files, data=data, auth=auth)
    print(r.status_code)
    print(json.loads(r.text))
    

    Output:

    200
    {'fileName': 'foo.txt',
     'nodeRef': 'workspace://SpacesStore/37a96447-44b0-4aaa-b6ff-98dae1f12a73',
     'status': {'code': 200,
      'description': 'File uploaded successfully',
      'name': 'OK'}}