is it possible to upload a image in confluence

noor basha December 18, 2019

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
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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.

noor basha December 19, 2019

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

noor basha December 19, 2019

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

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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
Rajpv May 23, 2022

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

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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

Rajpv May 24, 2022

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

0 votes
Rajpv May 24, 2022

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
AUG Leaders

Atlassian Community Events