It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Can i update file attachments using the rest api

James Woolfenden Jun 24, 2014

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

2 votes
Ana Nelson Aug 13, 2014

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

J D Nov 03, 2014

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

1 vote
J D Nov 03, 2014

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

936 views 0 19
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you