Missed Team ’24? Catch up on announcements here.

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

need to downlload attachment from jira via restapi

vineeth pv September 27, 2023

need to downlload attachment from jira via restapi

sample code mentioned below

using RestSharp;
using System;
using System.IO;
using System.Net;

class Program
{
static void Main(string[] args)
{

var username = "username";
var password = "Token";
var baseUrl = "https://vineethpv.atlassian.net"; // Replace with your Jira instance URL
var issueKey = "TEST-1"; // Replace with your Jira issue key
var attachmentId = "10000"; // Replace with the attachment ID
var attachmentFilename = "homeBanner.jpg"; // Replace with the attachment filename

// Step 1: Authenticate with Jira
var client = new RestClient($"{baseUrl}/rest/api/2/issue/{issueKey}");

// Set basic authentication using the AddDefaultHeader method
client.AddDefaultHeader("Authorization", $"Basic {Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"))}");

var request = new RestRequest();
request.Method = Method.GET; // Set the HTTP method to GET
request.AddHeader("Content-Type", "application/json");
request.AddParameter("fields", "attachment");

IRestResponse response = client.Execute(request);


if (response.StatusCode == HttpStatusCode.OK)
{
// Step 2: Parse attachment information
dynamic issueData = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content);
var attachments = issueData.fields.attachment;
foreach (var attachment in attachments)
{
if (attachment.filename == attachmentFilename)
{
//var attachmentUrl = attachment.content;
//var baseUri = new Uri(baseUrl);

//var attachmentUri = new Uri(baseUri, attachmentUrl);

//// Step 3: Download attachment
//var attachmentClient = new RestClient(attachmentUri);
//var attachmentUrl = attachment.content;
var attachmentUrl = "https://vineethpv.atlassian.net/secure/attachment/10000/homeBanner.jpg";
// Step 3: Download attachment
var attachmentClient = new RestClient(attachmentUrl);

//// Step 3: Download attachment
//var attachmentClient = new RestClient(attachmentUrl);

// Set basic authentication for attachmentClient as well
attachmentClient.AddDefaultHeader("Authorization", $"Basic {Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"))}");

var attachmentRequest = new RestRequest();
attachmentRequest.AddHeader("User-Agent", "Test");
attachmentRequest.Method = Method.GET; // Set the HTTP method to GET

IRestResponse attachmentResponse = attachmentClient.Execute(attachmentRequest);

if (attachmentResponse.StatusCode == HttpStatusCode.OK)
{
// Step 4: Save the attachment to a file
File.WriteAllBytes(attachmentFilename, attachmentResponse.RawBytes);
Console.WriteLine($"Attachment '{attachmentFilename}' downloaded successfully.");
}
else
{
Console.WriteLine($"Error downloading attachment: {attachmentResponse.Content}");
}
}
}
}
else
{
Console.WriteLine($"Error: {response.Content}");
}
}
}

but getting this error, why?

"StatusCode: Forbidden, Content-Type: application/json; charset=UTF-8, Content-Length: -1)"

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events