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

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"

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

Have you tried using the following documented api ?{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


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


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?


req = requests.Session()
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(" ")
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;} {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 ~~~~


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


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 Rehioui

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Thursday in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

302 views 0 12
Read article

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