Why Unsupported Media Type Error when attaching xlsx file to Confluence?
Asked by Marisa Voss:

Disclaimer: I've left certain information out of the code to be kept anonymous. Also using python3 with a combination of modules like pandas, openpyxl, StyleFrame, xlrd and xlwt for formatting the Excel sheet.

Every time I try to change the headers or media-type Confluence does not accept it. I know that I'm in the right space as far as Confluence is concerned. The file is not corrupted since I can open it in Excel. If anyone can help me on this I would greatly appreciate it.

My code is below:

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 \~\~\~\~')

The Error I receive:

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


Answers:
author: Nic Brough -Adaptavist-
body: It looks like Kenny may have found an answer for a different application, and probably posted a link that would not really have helped you.

I'm afraid I don't know the right answer either, but I do know that there is nothing wrong with your file or process, as I've run into the same thing. It is definitely something in your headers, and of them it is either the Content_Type and/or X-Content-Type-Options.

But as I typed that, I realised the most likely culprit! Try Content-Type, not Content_Type (if it's not that, then have another look at the values in those two)

comments: author: Marisa Voss
body: You right! Content-Type was definitely the problem. When a typo is the hardest bug to find smh.

Thank you.


author: kennyngkk
body: * removed as is incorrect

comments: author: Marisa Voss
body: What is correct?


author: Marisa Voss
body: [@kennyngkk](/t5/user/viewprofilepage/user-id/337155) Can you be more specific as to what is wrong and what you removed please?


author: kennyngkk
body: [@Marisa Voss](/t5/user/viewprofilepage/user-id/1208390) sorry that it took me too long to get back to you as it my fault that my . activity feed got flooded and its my fault. Yes, [@Nic Brough -Adaptavist-](/t5/user/viewprofilepage/user-id/781454) was right. I got the wrong document for a different product and we are glad that you were able to resolve the issue on your end.


Tags: python

atl-adm

attachment

error

ms-excel