Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Overwrite an attachment in Confluence using REST API

Hi,
I have an embedded Excel sheet on a page in Confluence (version 5.10.4) that I want to update on a daily basis. When I use the REST-api as described below i cant overwrite an existing attachment. Is there a work around for this? 

curl -D- -u user:pass -X POST -H "X-Atlassian-Token: no-check" -F "file=@export.xlsx" https://wiki.xyz.com/rest/api/content/63459897/child/attachment/

HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-ASEN: SEN-2412214
Set-Cookie: JSESSIONID=5F3AA70B086187DF857F2F7C43350B6; Path=/; HttpOnly
X-Seraph-LoginReason: OK
X-AUSERNAME: user
Cache-Control: no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 20 Dec 2017 10:16:37 GMT
X-Cnection: close
Set-Cookie: Site=!w9E67t/RiK/og3NjjX1iLPx+upVsmJEKY2C/ELZac2NYeWE5o8xzX588IVwH5yfISRMm6Cuh1P4=; path=/; Httponly; Secure

{"statusCode":400,"data":{"authorized":false,"valid":true,"errors":[],"successful":false,"notSuccessful":true},"message":"Cannot add a new attachment with same file name as an existing attachment: export.xlsx"}

 

1 answer

1 accepted

1 vote
Answer accepted
Hasnae Atlassian Team Dec 20, 2017

Have you tried using the following documented api ?

https://docs.atlassian.com/atlassian-confluence/REST/6.5.2/#content/{id}/child/attachment-updateData

(which I believe is also compatible with 5.10.4)

You basically need the attachment ID to be able to update it

Hi,

The update method worked when I used the Attachment-ID.

Thanks!

How come the following code doesn't work? It keeps telling me I'm updating Unsupported Media Type... but this person was able to upload an .xlsx file. I've been struggling with this problem for weeks trying different variations of this header. I know it's not the same command they are using above, but there has to be a way to do the same process in python with the REST API, right?

CODE FOLLOWS: 

req = requests.Session()
print("Hello")
url='https://confluence.<mycompany>.com/rest/api'
base_url = url
att = {'file': open("items-list2.xlsx", 'rb')}
headers = {"X-Atlassian-Token": "nocheck", "Content_Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "X-Content-Type-Options": "nosniff", "Accept" : "application/json"}
print("base: {} with spreadsheetml.sheet".format(base_url))
attID = 'myAttID'
r = req.get('{}/content/{}'.format(base_url, attID), headers=headers, auth = (username, password), files = att)

if (r.status_code < 300):
print(r.headers)

print(r)
print(r.text)
print(" ")
print('~~~~ failed to upload file ~~~~')

 

ERROR follows: 

~~~~~~~~~~~~
<Response [415]>
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.48 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 415 - Unsupported Media Type</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Unsupported Media Type</u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u></p><hr class="line"><h3>Apache Tomcat/8.0.48</h3></body></html>

~~~~ failed to upload file ~~~~

Hi, 

Attachment-ID it's the same as "entityid" in attachments page source. 

e.g.

curl -k -v -S -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@file.txt" -F "comment=new stats" "https://my-instance/rest/api/content/66682896/child/attachment/66682908/data" | python -mjson.tool

 

thx @Hasnae

Based on Szymon's answer, the "attachmentid" is the same as the "entityid" found by inspecting the page source. 

 

How do I retrieve the entityid programmatically? I need to automate the uploading of attachment.

Suggest an answer

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

Miss the Confluence demos at Team 21? Watch them here!

Phew, Atlassian Team 2021 blew by. With dozens of demos and a handful of keynotes, you may have missed some of the awesome Confluence Cloud sessions. Don't' worry, you can watch them all here or on Y...

349 views 2 6
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