Can i update file attachments using the rest api
Asked by James Woolfenden:

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.


Answers:
author: J D
body: 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>


author: Ana Nelson
body: 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](https://jira.atlassian.com/browse/CRA-328).

```
import osimport requestsimport uuidpage_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).contentimage2_content = requests.get(image2).contentdef 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        raiseattachment_name = str(uuid.uuid4()) + ".png"# initial upload to create a new attachmentpath = "content/%s/child/attachment" % page_idnew_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
```

comments: author: J D
body: 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


Tags: confluence-cloud