Can i update file attachments using the rest api

James Woolfenden June 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)

ta.

2 answers

2 votes
J D
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 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

2 votes
Ana Nelson August 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 = "http://upload.wikimedia.org/wikipedia/commons/7/79/01_-_Zahl_google_maps.PNG"
image2 = "http://upload.wikimedia.org/wikipedia/commons/1/18/02_-_google_maps.PNG"

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 = requests.post(
            url,
            files={
                'file':(canonical_name, contents,),
                'comment':"Uploaded automatically.",
                'minorEdit':"false"
                },
            auth=(username, password,),
            headers={'X-Atlassian-Token':'no-check'})
    try:
        return response.json()
    except Exception:
        print response.text
        raise

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events