JIRA Rest API Login using C#

Viewed 35355

I've written below C# code to login to JIRA Rest API:

var url = new Uri("http://localhost:8090/rest/auth/latest/session?os_username=tempusername&os_password=temppwd");
var request = WebRequest.Create(url) as HttpWebRequest;
if (null == request)
{
 return "";
}
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = 200;
request.KeepAlive = false;
using (var response = request.GetResponse() as HttpWebResponse)
{
}

When I execute this, application just goes on running without returning any response. Please suggest if this is the right way of calling JIRA Login using REST API

6 Answers

For posting multipart content in Rest I use Tiny.RestClient.

var client = new TinyRestClient(new HttpClient(), "http://localhost:8090");

var strResult = await client.PostRequest("rest/auth/latest/session).
WithBasicAuthentication("username", "password")
ExecuteAsStringAsync();
static void Main(string[] args)
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers.Add("Authorization", "Basic " + GetEncodedCredentials());

        string tasks = wc.DownloadString("yourjiraurl/search?jql=task=bug");
        var taskdetails = JsonConvert.DeserializeObject<TaskDetails>(tasks);
    }
}

static string GetEncodedCredentials()
{
    string mergedCredentials = string.Format("{0}:{1}", "UserName", "Password");
    byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
    return Convert.ToBase64String(byteCredentials);
}
Related