Updating binary data of attachment via REST API produces blank version

al_visnyai
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 11, 2020

Good Day: I have created a Python3 script to manipulate subpages and attachments on a Confluence system.  The script can successfully list, create, and delete subpages, and can successfully list, upload, and delete attachments.  However, when the script attempts to update the binary data of an attachment, a revision is made but the content of the revision is blank.  (The original version retains the original content.)

When the revision is made via the script, the operation shows a status code of 200, and the process appears to work, but the content is blank nevertheless.

Here is the relevant code block:

url = 'https://<host>/rest/api/content/' + contentID + '/child/attachment/' + attachmentID + '/data'

headers = {
'Accept': 'application/json',
'X-Atlassian-Token': 'nocheck'


files = {'file': open(tempfile_path + '/' + file_to_copy, 'rb')}

r = requests.post(url, files = files, headers = headers, auth = (username, password))

When I use a browser to upload the revised file, the updated content appears on the revision.

Any assistance would be appreciated.  Please bear with me, I am quite new to Confluence.

Thank you. 

1 answer

1 accepted

0 votes
Answer accepted
al_visnyai
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 6, 2020

After further testing and troubleshooting, a coding logic error in the script was discovered.

The following line:

files = {'file': open(tempfile_path + '/' + file_to_copy, 'rb')}

was not being called as part of the update section; instead, the variable "files" was being referred in the call to upload the file.  Since the initial call to the "files" line completed and pointed to the end of the file, the variable was blank and therefore the script updated the post with a "blank" attachment.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events