Unable to bring back list of projects

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?

 

1 answer

0 votes
Warren Levy Community Champion Jul 10, 2017

Hi Sean

Where you set the baseUrl, you've used http:// instead of https://

Adding the s in should start returning results

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

20,655 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you