【发布时间】:2016-05-19 02:16:06
【问题描述】:
我正在尝试使用 Python 请求通过 REST API 将附件上传到 Confluence。我总是收到“415 不支持的媒体类型”错误或“500 内部服务器错误”,具体取决于我发送请求的方式。
有一些关于如何使用其他语言执行此操作的信息,或者通过现已弃用的 XMLRPC API 使用 Python,或者对于行为似乎略有不同的 JIRA REST API。
根据所有这些信息,代码应该是这样的:
def upload_image():
url = 'https://example.com/confluence/rest/api/content/' + \
str(PAGE_ID) + '/child/attachment/'
headers = {'X-Atlassian-Token': 'no-check'}
files = {'file': open('image.jpg', 'rb')}
auth = ('USR', 'PWD')
r = requests.post(url, headers=headers, files=files, auth=auth)
r.raise_for_status()
缺少的是正确的内容类型标头。那里有不同的信息:
- 为文件使用正确的内容类型,在本例中为
image/jpeg - 使用
application/octet-stream - 使用
application/json - 使用
multipart/form-data
(我使用的 Confluence 版本是 5.8.10)
【问题讨论】:
标签: python rest python-requests confluence-rest-api