I'm playing around with the JIRA REST API and I'm having some problems connecting to my JIRA and bringing back the 2 sample projects I have set up.
Below is the complete code from my class 'JIRAManager.cs' which is trying to use to the REST API to connect to my JIRA site.
public enum JiraResource { project } public class JIRAManager { private const string baseUrl = "http://samplejira1.atlassian.net/rest/api/2/"; private string _userName; private string _password; public JIRAManager(string userName, string password) { _userName = userName; _password = password; } /// <summary> /// Runs a query towards the JIRA REST api /// </summary> /// <param name="resource">The kind of resource to ask for</param> /// <param name="argument">Any argument that needs to be passed, such as a project key</param> /// <param name="data">More advanced data sent in POST requests</param> /// <param name="method">Either GET or POST</param> /// <returns></returns> public string RunQuery(JiraResource resource, string argument = null, string data = null, string method = "GET") { string url = string.Format("{0}{1}/", 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(); } return result; } public List<ProjectDescription> GetProjects() { List<ProjectDescription> projects = new List<ProjectDescription>(); string projectsString = RunQuery(JiraResource.project); return JsonConvert.DeserializeObject<List<ProjectDescription>>(projectsString); } private string GetEncodedCredentials() { string mergedCredentials = string.Format("{0}:{1}", _userName, _password); byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials); return Convert.ToBase64String(byteCredentials); } }
This is done in a console application so my 'Program.cs' uses the above like so:
static void Main(string[] args) { Console.WriteLine("Hello and welcome to a Jira Example application!"); #region Create manager Console.Write("Username: "); string username = Console.ReadLine(); Console.Write("Password: "); string password = Console.ReadLine(); JIRAManager manager = new JIRAManager(username, password); #endregion Console.Clear(); List<ProjectDescription> projects = manager.GetProjects(); Console.WriteLine("Select a project: "); for (int i = 0; i < projects.Count; i++) { Console.WriteLine("{0}: {1}", i, projects[i].Name); } Console.Write("Project to open: "); string projectStringIndex = Console.ReadLine(); int projectIndex = 0; if (!int.TryParse(projectStringIndex, out projectIndex)) { Console.WriteLine("You failed to select a project..."); Environment.Exit(0); } ProjectDescription selectedProject = projects[projectIndex]; Console.WriteLine("You selected {0}", selectedProject.Name); Console.Read(); }
However, when I run this and enter my username and password, I merely get a blank response when I'm expecting a list of my projects. What is it I am missing that is stopping this from working as intended?
Hi Sean
Where you set the baseUrl, you've used http:// instead of https://
Adding the s in should start returning results
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.