I am trying to upload an attachment to Confluence using REST API from python. I get 415 error: unsupported media type.
I understand the content-type is not correct, but I tried some content types and got the same error:
import requests
print("start....................")
url = 'https://confluence.XXX.com/confluence/rest/api/content/' + str(108458915) + '/child/attachment/'
TOKEN = 'XXXXXXXX'
headers = {'X-Atlassian-Token': 'no-check',
"Authorization": f"Bearer {TOKEN}"
} #no content-type here!
file = 'my_file.jpeg'
content_type = 'multipart/form-data'
files = {'file': (file, open(file, 'rb'), content_type)}
print("request....................")
r = requests.post(url, headers=headers, files=files, verify='confluence.XXX.crt')
r.raise_for_status()
I tried the following content-types: 'image/jpeg'
'application/json'
'application/xml'
'application/octet-stream'
'multipart/form-data'
but get the same 415 error:
requests.exceptions.HTTPError: 415 Client Error: for url: https://confluence.XXX.com/rest/api/content/108458915/child/attachment/
I didn't find what is the problem with my first code, but the following code fixed the issue:
import requests
url = "https://confluence.XXX.com/rest/api/content/108458915/child/attachment"
certificate = 'confluence_certificate.cer'
TOKEN = 'XXXXXXXX'
files = {'file': ('my_file.jpeg', open('my_file.jpeg', 'rb'))}
headers = {'X-Atlassian-Token': 'no-check',
'Authorization': f"Bearer {TOKEN}"}
response = requests.post(url, files=files, headers=headers, verify=certificate)
print(response.status_code)
print(response)