使用Python和Alfresco API上传文件

2024-09-26 18:18:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Alfresco和Python中遇到了一个问题:我想用Python直接上传一个新文件。每次我尝试我都会得到一个没有细节的http500错误。。。在

我首先尝试使用CURL,以确保它能正常工作,并且文件上传时没有问题。在

我使用了以下curl命令行:

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

在Python脚本中,我尝试了PyCurl,现在尝试了简单的urllib。我认为问题出在文件作为参数的方式。在

使用PyCurl的Python代码:

^{pr2}$

此代码示例将导致:

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

有人知道怎么做到吗?在


Tags: 文件the代码命令行url错误errorcurl
1条回答
网友
1楼 · 发布于 2024-09-26 18:18:24

您可以使用非常简单的库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))

输出:

^{pr2}$

相关问题 更多 >

    热门问题