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

This widget could not be displayed.
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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

286 views 5 0
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