I am trying to upload an attachment to Confluence via the REST API, using Python Requests. I always get either a "415 unsupported media type" error or a "500 internal server error", depending on how I send the request.
There are several bits of information about how to do this with other languages, or using Python via the now deprecated XMLRPC API, or for the JIRA REST API which seems to behave slightly different.
This is how, according to all that information, the code should look like:
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()
What is missing is the correct content-type header. There is different information out there:
- use the correct content-type for the file, in this case
image/jpeg
- use
application/octet-stream
- use
application/json
- use
multipart/form-data
(The Confluence version I am using is 5.8.10)