Call JIRA Rest API using C Sharp

Does any have an example of c sharp code that will allow me to call the JIRA Rest API and create an issue in JIRA?

Hey Robert, is this for the cloud or server version of Jira? If the latter, I found a thread on Connecting to the Jira Server REST API via C# that includes some example code you might find helpful. Let me know how you go with it.

1 Like

Here are the two functions I used to get the HttpClient from the Cloud or Server:

static HttpClient GetHttpClientForCloud()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(BaseUrlCLOUD);
client.DefaultRequestHeaders.Add(“Authorization”, $“Bearer {ApiToken}”);
client.DefaultRequestHeaders.Add(“X-Atlassian-Token”, “no-check”);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
return client;
}

static HttpClient GetHttpClientForServer()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(BaseUrlSERVER);
    client.BaseAddress = new Uri(BaseUrlSERVER);
    client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check");
    string authHeader = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{Username}:{Password}"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return client;
}

Here’s a function to get and save the issue:
SEE:
JIRA 7.6.1

static async Task GetIssueByKey(string issueIdOrKey, bool doPrint = false, bool saveAttachments = false, bool saveResponse = false)
{
Console.WriteLine($“RUNNING: GetIssueByKey({issueIdOrKey}, {doPrint}, {saveAttachments}, {saveResponse})…”);
try
{
using (HttpClient client = GetHttpClientForServer())
{
string endpoint = $“{BaseUrlSERVER}/rest/api/2/issue/{issueIdOrKey}”;
Console.WriteLine($" Endpoint: {endpoint}");

            HttpResponseMessage response = await client.GetAsync(endpoint);
        
            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest || response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                Console.WriteLine("          Bad Request: " + response.ReasonPhrase);
                Console.WriteLine(response);
            }
            else
            {
                Console.WriteLine($"          Response (SUCCESS): {response.StatusCode} {response.ReasonPhrase}");
                // If saveResponse is enabled, save the response to a file
                if (saveResponse)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var fileName = $"JIRA - Issue {issueIdOrKey}.json";
                    SaveResponse($"{OutputPath}{fileName}", responseBody);
                }
            }

            // Return the response here
            return response;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
        return null; // You can return null or handle the exception as needed
    }
}