As I’m working a lot through JIRA I thought I’d try to communicate with it using C# and the REST api it’s exposing. This was not completely simple and it’s obvious they prefer people using java. There is a library available if you are a java user which simplifies a lot but there is none for C#. I will probably write more than a few articles about the subject as I’ve just installed JIRA on my computer.
The first thing you need to know about communicating with the REST api is that the full api documentation is found here. This will be your bestest friend ever when developing against it.
The second thing you need to know is that they used to have a SOAP api but since 4.something that’s deprecated in favor of the new REST api.
A standard JIRA REST call looks like this:
http://host:port/context/rest/api-name/api-version/resource-name
If you want to try it out you can use an open project on atlassian:
https://jira.atlassian.com/rest/api/latest/issue/JRA-9
The response is given in JSON format which is short and nice and easy to parse using the JSON.NET library. In this first article however we’ll just ensure that we can connect to JIRA and get some data in return.
When we use the open atlassian project in the link above we view the issue as an anonymous user. That takes us only so far so one of the most important parts of the connection is to send the proper credentials. We’ll create a console project which asks the user for a username and password which it uses to fetch all projects available to that user. That’s enough to get one started:
public enum JiraResource { project } public class JiraManager { private const string m_BaseUrl = "http://localhost.:8080/rest/api/latest/"; private string m_Username; private string m_Password; public JiraManager(string username, string password) { m_Username = username; m_Password = password; } public void RunQuery( JiraResource resource, string argument = null, string data = null, string method = "GET") { string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString()); if (argument != null) { url = string.Format("{0}{1}/", url, argument); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ContentType = "application/json"; request.Method = method; if (data != null) { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(data); } } string base64Credentials = GetEncodedCredentials(); request.Headers.Add("Authorization", "Basic " + base64Credentials); HttpWebResponse response = request.GetResponse() as HttpWebResponse; string result = string.Empty; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } Console.WriteLine(result); } private string GetEncodedCredentials() { string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password); byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials); return Convert.ToBase64String(byteCredentials); } } |
And then we use it in our programs.cs file:
class Program { static void Main(string[] args) { Console.WriteLine("Hello and welcome to a Jira Example application!"); Console.Write("Username: "); string username = Console.ReadLine(); Console.Write("Password: "); string password = Console.ReadLine(); JiraManager manager = new JiraManager(username, password); manager.RunQuery(JiraResource.project); Console.Read(); } } |
The code could do with some refactoring but this is just a simple example. This, if run, should generate something like this (if you log in correctly and have some projects):
Things to notice in the code:
- The Authorization header that we use to send our credentials look like this: “Authorization” : “Basic [BASE64 ENCODED CREDENTIALS]“
- Content-type must be application/json as we send and receive data using the json format
- The RunQuery method is more generic than required but I’m going to re-use it in future articles. It allows for you to attach data and extra parameters which is sometimes needed
- The URL goes to localhost. (notice the dot ‘.’ after localhost). This is to make the transaction visible in Fiddler when I monitor the communication
If you need to monitor the communication with Fiddler you have to, as mentioned in the last line above, add a dot after ‘localhost’ in your URL (if you are running on your local computer, otherwise never mind). Open Fiddler2 and monitor the communication and you’ll see something like this:
Read more in the next article: JIRA: Basic C#/JIRA fetching and displaying projects


Hi Magnus Ferm,
This is a very nice article.. thanks for it..
I am very eager to know how to get issues from Jira using the REST API’s.
Your reply with the code snippet is most awaited.
Thanks,
Suhas
Hi Suhas
Thanks for the kind words. I will get the next article up as soon as possible!
/Magnus
Hi,
Thanks for the help, nice explained article. I am trying to pass JIRA issues statuses through REST API. Would you know any article for it please ? Thanks
Well, I have written two more articles on the subject which may give you an idea on how to accomplish that:
I’ve put up an article now on how to search for issues as a follow up to this one:
http://maffelu.net/jira-basic-cjira-searching-for-issues/
Pingback: JIRA: Basic C#/JIRA fetching and displaying projects | Maffelu.net
I’m having trouble using this code to connect to an online Jira domain with authentication.
I stripped down RunQuery function to a very basic form:
public void RunSimpleQuery(string method = “GET”)
{
string url = m_BaseUrl;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = “application/json”;
request.Method = method;
string base64Credentials = GetEncodedCredentials();
request.Headers.Add(“Authorization”, “Basic ” + base64Credentials);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
Console.WriteLine(result);
}
I if comment out the authentication lines and switch the baseURl to: “https://jira.atlassian.com/rest/api/latest/issue/JRA-9″; I can quickly get information, so I know the system works when accessing the web. Using cURL I can access the right domain with my username and password but I can’t get anything to work in C#. My url is:”https://jira.****.com/rest/api/latest/issue/SSI-39″ where **** is the domain I am connecting to. Everytime I try to connect the program waits for about a minute or two and then tells me a WebException was unhandled and that “The operation has timed out”.
Any thoughts or suggestions?
Have you enabled the RPC plugin in JIRA for remote access?
Check out the following linke: https://confluence.atlassian.com/display/JIRA042/Creating+a+SOAP+Client#
It is enabled. Also that link if for SOAP not REST.
I set up a trial JIRA server and the code worked. I think I figured out the issue (but not how to fix it yet). The server I am attempting to connect to is https not http so I probably need to configure the connection differently.