Overwrite an attachment in Confluence using REST API

Tobbe G I'm New Here Dec 20, 2017

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

0 vote

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

Tobbe G I'm New Here Dec 21, 2017

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published 8 hours ago in Confluence

Think you know shares vs. @mentions in Confluence? Take this collab quiz.

To anyone who doubts that Atlassians are a little too obsessed with collaboration, and tools related thereto, let me describe a recent discussion we had (which took place on our internal Confluence, ...

85 views 2 4
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