How to post attachments to confluence page

Hi there, I have searched but with no luck so far? Is it possible to post .png images to a confluence page using either Rest API or Java API?The .png images are sourced from a 3rd party tool and the idea is to build a plugin that will export and import (post) the .png images to Confluence pages?

Any help will will be most welcome? 

3 answers

This widget could not be displayed.

Hello Etienne,

Thank you for your inquire.

You could be running the following in Python, for instance:

# ===============
# upload attachment
# ===============
import requests
import json

# [CONFLUENCE-BASE-URL], i.e.: http://localhost:8090
# [CONTENT-ID], get content ID by running '[CONFLUENCE-BASE-URL]/rest/api/content'
url = '[CONFLUENCE-BASE-URL]/rest/api/content/[CONTENT-ID]/child/attachment'
headers = {"X-Atlassian-Token": "nocheck"}
data = {"comment":"this is my file"}


# please, uncomment to attach inline content
#files = {'file': ('report.xml', '<?xml version="1.0" encoding="UTF-8"?><note><to>RECIPIENT</to><from>SENDER</from><heading>ATTACHMENT</heading><body>CONTENT</body></note>')}

# please uncomment to attach external file
#files = {'file': open('text.txt', 'rb')}
#files = {'file': open('image.png', 'rb')}

# upload file to page
# [USERNAME], i.e.: admin
# [PASSWORD], i.e.: admin
r = requests.post(url, data=data, auth=('[USERNAME]', '[PASSWORD]'), files=files, headers=headers)
print(r.status_code)
print(r.text)

Alternatively, you could be running the following cURL command:

# [USERNAME], i.e.: admininstrator
# [DOMAIN], i.e.: mycompany
# [CONTENT-ID], i.e.: 9666562
curl -D- -u [USERNAME] -p -H "X-Atlassian-Token: nocheck" -X POST -F file=@/full/path/to/image.png https://[DOMAIN].atlassian.net/wiki/rest/api/content/[CONTENT-ID]/child/attachment

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

Kind regards,
Rafael P. Sperafico
Atlassian Support

Thanks for the very useful python example.  I'm curious though, it seems to work for me when I first upload but if my change the file I want to attach and then run the python program again, I get a 400 Bad Request.

 

Is there some magic needed to allow updating an attachment?

This widget could not be displayed.

Using the Confluence Command Line Interface (CLI) might make it easier - use the addAttachment action. 

This widget could not be displayed.

Moving attachments from its current space to a different space:

Using curl, the following can be used to attach the file to a page in a different space:

curl -v -u 'admin:XXX’ -X PUT -d '{"id":"attXXX”,”type":"attachment","title”:”xxx.jpeg","status":"current","ancestors":[],"version": {"number":"2"},"container": {"id": "34514","type": "page","status": "current","title": "Andro Homepage!","space": {"id": 34513,"key": “AR”,”name": "Andro"}}}' -H 'X-Atlassian-Token:<access-token>' -H 'Content-Type:application/json' https://XXX.atlassian.net/wiki/rest/api/content/<parent page id>/child/attachment/attXXX | python -m json.tool

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...

475 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