Overwrite an attachment in Confluence using REST API
Asked by Tobbe G:

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


Answers:
author: Hasnae
body: 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

comments: author: Tobbe G
body: Hi,

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

Thanks!


author: Marisa Voss
body: 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](https://community.atlassian.com/t5/Confluence-questions/Why-Unsupported-Media-Type-Error-when-attaching-xlsx-file-to/qaq-p/770216)'  
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 \~\~\~\~


author: Szymon Hubar
body: 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](/t5/user/viewprofilepage/user-id/767294)


author: Erik Lewis
body: 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.


author: Pete-K
body: If you have installed the JSON-Converter, you can use this VBA-code as template for your own adjustments:

```
Function FindAttachmentIdByName(apiResponse As String, fileName As String) As String
Dim jsonResponse As Object
Set jsonResponse = JsonConverter.ParseJson(apiResponse)

Dim results As Object
Set results = jsonResponse("results")

Dim attachment As Object
For Each attachment In results
If attachment("title") = fileName Then
FindAttachmentIdByName = attachment("id")
Exit Function
End If
Next attachment

' R?ckgabewert, falls die Datei nicht gefunden wurde
FindAttachmentIdByName = ""
End Function
```

The JSON-Converter for VB/VBA can be found at Tim Hall's page - <https://github.com/VBA-tools/VBA-JSON>

This helped me alot to implement a VBA-Code to upload file attachments to my Confluence-Pages.

Greetinx from Germoney


Tags: atl-adm

attachment

embed

rest

rest-api