In this article, we will discuss how to connect to ALM using new rest API. We will use C#.Net for this article.
For accessing ALM using rest API first we will have to create a web request and set username and password for the web request.
String ALMBASEURL = "" WebRequest request = WebRequest.Create(ALMBASEURL + "/api/authentication/sign-in"); SetBasicAuthHeader(request, "", "");
SetBasicAuthHeader is a user-defined function which is below
public static void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string authInfo = userName + ":" + userPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); request.Headers["Authorization"] = "Basic " + authInfo; }
After this we need to get the web response for this request, We will convert the response into HTTPWebResponse to figure out if the Login was successful
WebResponse response = request.GetResponse(); HttpWebResponse ORES = (HttpWebResponse)response; if (ORES.StatusCode == HttpStatusCode.OK) { //Login is successful }
Rest response will have few authentication cookies and we will need these cookies for sending any other request using the session you just created.
There are 4 Cookies returned by the Login Request
- XSRF-TOKEN
- LWSSO_COOKIE_KEY
- QCSession
- ALM_USER
We need to find these cookies from the response and use these cookies in any other request
Below is the code to find the cookies and save them in variable
public static Boolean LoginALM() { try { String OREQ = ""; String SessionCookieName = "XSRF-TOKEN"; String SessionCookieValue = ""; String AuthenticationCookieName = "LWSSO_COOKIE_KEY"; String AuthenticationCookieValue = ""; String QCSESSIONNAME = "QCSession"; String QCSESSIONValue = ""; String ALMUSERNAME = "ALM_USER"; String ALMUSERValue = ""; String ALMBASEURL = "ALM Server URL that ends with qcbin" WebRequest request = WebRequest.Create(ALMBASEURL + "/api/authentication/sign-in"); SetBasicAuthHeader(request, "", ""); WebResponse response = request.GetResponse(); HttpWebResponse ORES = (HttpWebResponse)response; if (ORES.StatusCode == HttpStatusCode.OK) { for (int i = 0; i < response.Headers.Count; ++i) { if (response.Headers.Keys[i] == "Set-Cookie") { OREQ = response.Headers[i]; SessionCookieValue = FindCookie(SessionCookieName, OREQ); AuthenticationCookieValue = FindCookie(AuthenticationCookieName, OREQ); QCSESSIONValue = FindCookie(QCSESSIONNAME, OREQ); ALMUSERValue = FindCookie(ALMUSERNAME, OREQ); break; } } return true; } else return false; } catch (Exception ex) { Console.WriteLine("Error occurred while trying to login to ALM : " + ex.Message.ToString()); return false; } } public static String FindCookie(String CookieName, String OREQ) { foreach (String Str in OREQ.Replace("Path=/", "").Split(';')) { if (Str.Contains(CookieName)) { return Str.Split('=')[1]; } } return null; }
Once we have all of the cookies stored in the variables we can use it for sending any other requests.
Now we will create a function to set all of the Cookies to the new web requests
Below is the code
public static Boolean SetCookieContainer(ref WebRequest ORequest, String SessionCookieValue, String AuthenticationCookieValue, String QCSESSIONValue, String ALMUSERValue)
{
try
{
String SessionCookieName = "XSRF-TOKEN";
String AuthenticationCookieName = "LWSSO_COOKIE_KEY";
String QCSESSIONNAME = "QCSession";
String ALMUSERNAME = "ALM_USER";
Uri target = new Uri();
Cookie OCK = new Cookie(SessionCookieName, SessionCookieValue) { Domain = target.Host };
Cookie OCK1 = new Cookie(AuthenticationCookieName, AuthenticationCookieValue) { Domain = target.Host };
Cookie OCK2 = new Cookie(QCSESSIONNAME, QCSESSIONValue) { Domain = target.Host };
Cookie OCK3 = new Cookie(ALMUSERNAME, ALMUSERValue) { Domain = target.Host };
HttpWebRequest httpRequest = ORequest as HttpWebRequest;
httpRequest.CookieContainer = new CookieContainer();
//httpRequest.Accept = “application/json”;
httpRequest.CookieContainer.Add(OCK1);
httpRequest.CookieContainer.Add(OCK);
httpRequest.CookieContainer.Add(OCK2);
httpRequest.CookieContainer.Add(OCK3);
return true;
}
catch (Exception Ex)
{
Console.WriteLine(“Error Occurred while setting the cookie container : ” + Ex.Message.ToString());
return false;
}
}
Now we will use all of the code to Logout from ALM
public static Boolean LogOutALM() { WebRequest request = WebRequest.Create(ALMBASEURL + "/api/authentication/sign-out"); SetCookieContainer(ref request); WebResponse response = request.GetResponse(); HttpWebResponse ORES = (HttpWebResponse)response; if (ORES.StatusCode == HttpStatusCode.OK) return true; else return false; }
That’s it, now you have completed login and logout using REST API.
Let me know if you have any question