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 vote
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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

575 views 6 12
Read article

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