Uploading and updating an Image to Confluence using the REST API

If you have automated systems sometimes it is useful to have them interact with your documentation from time to time.  Using the Confluence REST API you can upload an image as an attachment to the page and later update it if required.  If you follow the Atlassian documentation you end up getting a denied request for the update as it wishes for the new file to have a different filename.
Below are the curl lines to post and then update the image.  Change your page ID and attachment ID as required:

curl -D- -k -u $user:$pass -p -H "X-Atlassian-Token: nocheck" -X POST \ 
 -F file=@cake.jpg https://confluence.domain.tld/confluence/rest/api/content/$page_id/child/attachment
curl -D- -k -u $user:$pass -p -H "X-Atlassian-Token: nocheck" -X POST \
 -F file=@cake.jpg https://confluence.domain.tld/confluence/rest/api/content/$page_id/child/attachment/$attach_id/data

In the first request you should have a JSON response with Results->ID->att123456 – the attach_id to be used above is just the numeric part of this string, i.e. 123456.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: