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)

ta.

2 answers

This widget could not be displayed.

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

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

This widget could not be displayed.

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
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

477 views 6 6
Join discussion

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