Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

"UnsupportedMediaType" when trying to upload a zip file to a Jira issue using RestSharp

I am trying to create an automated service for Jira where a zip file or .db3 file is uploaded to an existing Jira issue. This is my current code:

var client = new RestClient(".../rest/api/3/issue/"+issueKey);
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Authorization", "Basic ...");
request.AddHeader("X-Atlassian-Token", "nocheck");
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
ZipFile.CreateFromDirectory(path, zipPath);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("X-Content-Type-Options", "nosniff");
request.AddHeader("Accept", "application/zip");
request.AddFile("file", zipPath, "application/zip");
var response = client.Execute(request);

 When this request is sent, I receive "StatusCode: UnsupportedMediaType, Content-Type: text/html;charset=UTF-8, Content-Length: -1)" even though the content-type has been set to application/zip, I am not sure what is going wrong here. I know zip files can be uploaded directly into the issue, so it is not an issue with project permissions or custom restrictions. When adding on "/attachments" to the end of the RestClient url, MethodNotAllowed is returned or when "Content-Type" is set to "application/zip", UnsupportedMediaType is also returned. The documentation only talks about image or json files, I have have not seen anything for other file types. Any help here would be appreciated. 

1 answer

1 accepted

1 vote
Answer accepted
Andy Heinzer Atlassian Team Jul 14, 2021

Hi Joshua,

I see that you are trying to upload an attachment to a Jira Cloud issue, but are getting an unsupportedmediatype error in the process.  I think I can see the problem here.  Try removing your header of

request.AddHeader("Accept", "application/zip");

If it's still not working, then also remove the header of

request.AddHeader("X-Content-Type-Options", "nosniff");

I was able to recreate this problem when trying to use the accept header here.  I believe it is confusing the media service that accepts file attachments to see these headers in the request.  Check out our Jira Cloud REST API reference for this endpoint over in https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post it explains in some more details about which header is expected for a call like this, and it looks like your call has two additional unexpected headers here.

I tested this out in my own environment using curl and I found that I could upload attachments with an example like this one:

curl --request POST \
--url 'https://[yourCloudSite].atlassian.net/rest/api/3/issue/SCRUM-33/attachments' \
--header 'Authorization: Basic [redacted]' \
--header 'Content-Type: multipart/form-data' \
--header 'X-Atlassian-Token: no-check' \
-F "file=@testfile2.zip"

I also found that I could remove the header of "content-type" entirely and still successfully upload the file here, but that might also be on account of using the curl client to make this request. 

Try adjusting your call to remove these extra headers and let me know if you still run into any problems with this.

Andy

Thanks a ton for the help, Andy! Just removing the Content-Type and Accept entirely as headers worked. I only added X-Atlassian-Token as a header along with authorization. I also realized I didn't need to zip the file since it does support uploading .db3. My new code is below:

public void AddJiraAttachments(string issueKey, string filePath)
{
var client = new RestClient(".../"+issueKey+"/attachments");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("X-Atlassian-Token", "no-check");
request.AddHeader("Authorization", "...");
request.AddFile("file", filePath);
IRestResponse response = client.Execute(request);
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Atlympic Event: Jira Service Managemnt

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

179 views 0 3
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