Can i update file attachments using the rest api

I can upload files fine using the rest api

, curl -v -S -u admin:xxxx -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile2.txt" http://mypc:1990/confluence/rest/api/content/852011/child/attachment

however the rest api browser seems to say i can modify exsiting attachments

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt;minorEdit=true;comment=This is my updated File" http://myhost/rest/api/1/content/123/attachments/456/data
An example to upload the same file, with no comment:
curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" http://myhost/rest/api/1/content/123/attachments/456/data

I cant get the correct sysntax for this to work. Can you update exsiting attachments?

If not can i delete using the rest api. (im using ondemand confluence if only supported in local some other way im also interested)


2 answers

Here's a script which shows how to update attachments. Apparently the trick is to provide comment and minorEdit along with the file contents, otherwise there's an error. I filed a bug.

import os
import requests
import uuid

page_id = os.environ["CONFLUENCE_PAGEID"]
username = os.environ["CONFLUENCE_USERNAME"]
password = os.environ["CONFLUENCE_PASSWORD"]
url_base = os.environ["CONFLUENCE_URL"]

api_path = "/wiki/rest/api/"

image1 = ""
image2 = ""

image1_content = requests.get(image1).content
image2_content = requests.get(image2).content

def url_for_path(path):
    return "".join((url_base, api_path, path,))

def post_file(path, canonical_name, contents):
    url = url_for_path(path)
    print "uploading data to url", url
    response =
                'file':(canonical_name, contents,),
                'comment':"Uploaded automatically.",
            auth=(username, password,),
        return response.json()
    except Exception:
        print response.text

attachment_name = str(uuid.uuid4()) + ".png"

# initial upload to create a new attachment
path = "content/%s/child/attachment" % page_id
new_attachment = post_file(path, attachment_name, image1_content)
attachment_id = new_attachment['results'][0]['id']

print "Created new attachment %s, with id %s" % (attachment_name, attachment_id)

update_attachment_path = "content/%s/child/attachment/%s/data" % (page_id, attachment_id)
updated_attachment = post_file(update_attachment_path, attachment_name, image2_content)

print updated_attachment

can you re-post this in java pls? Or how you get this script running? I have kind of the same problem. I want to do such stuff in as rest-api plugin for confluence. Regards

Just the example with the comment and minorEdit works.

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt;minorEdit=true;comment=This is my updated File" http://myhost/rest/api/1/content/123/attachments/456/data

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Dec 18, 2018 in Confluence Cloud

Happy holidays from our team to yours!

Hi Community!  2018 was filled with changes for our team, both big and small, and we've taken a lot of time to both celebrate our wins and recognize areas of improvement. One thing that we're a...

498 views 3 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you