Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,815
Community Members
 
Community Events
165
Community Groups

is it possible to upload a image in confluence

Hi All

 

I am new to confluence, my task is to upload a multiple images to confluence page through rest call. can anyone give a sample example. i tried with curl but i am getting 405 error

 

curl -D- -u admin:password -X POST -H "X-Atlassian-Token: nocheck" -F "file=@host.txt" https://company.domain.com/rest/api/content/id/child/attachment

please any one help. 

Thanks in advance

 

 

2 answers

1 vote
DPK J Community Leader Dec 18, 2019

@noor basha  Welcome to the community, something like this might work for you.

curl -D- -u '<USER_NAME>:<PASSWORD>' \
-X POST \
-H 'X-Atlassian-Token: nocheck' \
-F 'file=@"<FILE_PATH>"' \
'<CONFLUENCE_BASE_URL>/rest/api/content/<PAGE_ID>/child/attachment'

And the response 405 implies method you are using i.e. POST is not applicable, so please check if your page is not blocked for editing etc.

Thanks for quick response sir, I tried this it gives response 200 but image not inserting. page is blank.
curl -D- -u 'admin:password' -X POST -H 'X-Atlassian-Token: nocheck' -F 'file=@"/root/host.txt"' 'https://company.com/rest/api/content/id/child/attachment'

import requests
def upload_image():
url = 'https://company.com/rest/api/content/' + \
str(id) + '/child/attachment/'
#headers = {'Content-Type: application/json'}
#headers = {'Content-Type': 'image/jpeg'}
headers = {"X-Atlassian-Token": "nocheck"}
content_type = 'image/jpeg'
file = 'download.jpg'
files = {'file': ('download.jpg', open('download.jpg', 'rb'),content_type)}
auth = ("admin", "password")
r = requests.post(url, headers=headers, files=files, auth=auth)
print(r.status_code)
upload_image()

I tried with this script also. in both cases I am getting 200 response but page is blank. please help me. its 2nd day I am working on this same

i tried with different approach then it is working fine. but my problem is the picture is not displaying in confluence page. picture is in attachments, i need to display in confluence page. 

please give some reference to display the picture with rest api

DPK J Community Leader Dec 19, 2019

Displaying attachment on confluence page is another part of story and you will need to fire separate APIs for this.

You need to put something like this in your page body using API,

<ac:image>
  <ri:attachment ri:filename="atlassian_logo.gif" />
</ac:image>

Here is an working API example, in first API I'm adding image `sample.png` as attachment and later in second API call I'm displaying that in page using Confluence Structure format in body.

 

curl -D- -u '<USERNAME>:<PASSWORD>' \
-X POST \
-H 'X-Atlassian-Token: nocheck' \
-F 'file=@"./sample.png"' \
'<CONFLUENCE_BASE_URL>/rest/api/content/<PAGE_ID>/child/attachment'

curl --request PUT \
--url '<CONFLUNCE_BASE_URL>/rest/api/content/<PAGE_ID>' \
--user '<USERNAME>:<PASSWORD>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"<PAGE_ID>",
"type":"page",
"title":"Sample Page",
"space":{"key":"<SPACE_KEY_OF_PAGE>"},
"body":{
"storage":{
"value":"<p>Adding image to page</p><ac:image><ri:attachment ri:filename=\"sample.png\" /></ac:image>",
"representation":"storage"
}
},
"version":{"number":<NEXT_VERSION_NUMBER>}
}'

Like Marcel Flipse likes this

Hi DPK J -

is "version":{"number":<NEXT_VERSION_NUMBER>} Mandatory??

How do we know the NEXT_VERSION_NUMBER from confluence page.. pls suggest.

Thanks,

Raj

DPK J Community Leader May 23, 2022

Hey,

Take a look at Get Content api response and you will get it.

 

https://docs.atlassian.com/ConfluenceServer/rest/7.17.2/#content-getContent

curl -k --request PUT \
--url 'https://confluence.global.standardchartered.com/rest/api/content/'<spacekeyname> \
--user 'myid:password' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"2377271484",
"type":"page",
"title":"NewPTPage",
"space":{"key":"CC"},
"body":{
"storage":{
"value":"<p>Adding image to page</p><ac:image><ri:attachment ri:filename=\"c:\Users\test\image\sb1.png\" /></ac:image>",
"representation":"storage"
}
},
"version":{"number":2}
}'

 

While executing the above cmd .. i dont any error in the git bash but png file is not attached in Confluence page

Hi DPK J -

i see the below curl cmd working as expected... 

when i post below request - i could the image rendered on the confluence page.

But when i try to render another image - it is overwriting the existing/first image on the confluence page.

 

My question is how do we apend the images in the confluence page instead of overwriting... Please suggest. thanks

 

curl --insecure -k --request PUT \
--url 'https://confluence.global.standardchartered.com/rest/api/content/2377271484' \
--user '1628:pwd' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"2377271484",
"type":"page",
"title":"NewPTPage",
"space":{"key":"CC"},
"body":{
"storage":{
"value":"<p>Adding image to page</p><ac:image><ri:attachment ri:filename=\"values.png\" /></ac:image>",
"representation":"storage"
}
},
"version":{"number":14}
}'

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Confluence: Where work and wellness meet

Feeling overwhelmed by the demands of work and life? With a 25% increase in the prevalence of anxiety and depression worldwide during the pandemic, for most of us, it’s a resounding yes . 🙋‍♀️ ...

833 views 10 23
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you