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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

need to downlload attachment from jira via restapi

Edited
vineeth pv
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Sep 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