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.
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Andy Heinzer This is my code but it's not working
string filePath = @"D:\file.zip";
if (!File.Exists(filePath))
{
throw new Exception("File not found at specified location");
}
string jiraNumber = "issueNo";
string url = $"{JiraBaseUrl}/issue/{jiraNumber}/attachments";
string username = "jira@abc.com";
string password = "password";
var client = new RestClient(url);
var request = new RestRequest(Method.Post.ToString());
request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")));
request.AddHeader("X-Atlassian-Token", "no-check");
//request.AddHeader("Content-Type", "multipart/form-data");
request.AddFile("file", filePath,"application/zip");
var response = client.Execute(request);
// Check if request was successful
if (response.IsSuccessful)
{
Console.WriteLine("File uploaded successfully.");
}
else
{
Console.WriteLine($"Failed to upload file. Error: {response.ErrorMessage}");
}
it's not working for me as giving error Failed to upload file.Error and nothing in response.ErrorMessage
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.