Quality Center Support and News
Showing results for 
Search instead for 
Do you mean 

How to access ALM data using REST API

Occasional Advisor

How to access ALM data using REST API

Hi all,
can anyone tell me how to access ALM data using REST API? Is there any java code example available?
Thanks in advance.
23 REPLIES
Frequent Advisor

Re: How to access ALM data using REST API

Hi,
You can find the ALM 11.0 REST API reference guide here:
http://support.openview.hp.com/selfsolve/manuals

http://support.openview.hp.com/selfsolve/document/KM997956/binary/ALM11.00_RE
ST_API.html?searchIdentifier=-765a98c0%3a12de84ca170%3a-27ac&resultType=document

In the documentation you will find examples how to use Rest resources.
The main entry point it the authentication after then you can login to project, create entities and more using the exposed resources.

To consume it from Java read here:
http://blogs.sun.com/enterprisetechtips/entry/consuming_restful_web_services_with

Thanks,
Elad
Occasional Contributor

Re: How to access ALM data using REST API

From: http://support.openview.hp.com/selfsolve/document/KM997956/binary/ALM11.00_RE
ST_API.html?searchIdentifier=-765a98c0%3a12de84ca170%3a-27ac&resultType=document

I can't see any examples of using Rest resources, Could you please be more specific?
Occasional Contributor

Re: How to access ALM data using REST API

[ Edited ]

I figured out. please see:

http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1484300

 

Edited by Bill Groot:

The above link is an old ITRC link and will not work. I have found the thread so to see it please click on this link:

 

http://h30499.www3.hp.com/t5/Quality-Center-Support-and-News/HP-ALM-rest-API-usage-issue-during-logi...

Visitor

Re: How to access ALM data using REST API

Unfortunately the last link is not valid anymore. Did someone manage to login via the Java API to HP-ALM? If yes may I ask you to send me the source code. Thanks a lot for any help.

Occasional Visitor

Re: How to access ALM data using REST API

Did you get the ALM REST API working?  I'm seeking the .jar file to import into java servlets.

Visitor

Re: How to access ALM data using REST API

Yes I found a way that is working. You can find the necessary files in the attached ZIP files.

Acclaimed Contributor Acclaimed Contributor

Re: How to access ALM data using REST API

>Unfortunately the last link is not valid anymore.

 

Looking at Justin's other topic, it's probably:

http://h30499.www3.hp.com/t5/Quality-Center-Support-and-News/HP-ALM-rest-API-usage-issue-during-logi...

Established Member

Re: How to access ALM data using REST API

When I download the manual i can see the TOC, but when I open the page I receive an error "Navigation to the webpage was canceled

What you can try:
Retype the address. "

Occasional Visitor

Re: How to access ALM data using REST API

I have developed a framework able to connect in ALM via restful. Please check it:

 

Api fo JAVA

 

http://code.google.com/p/alm-java-api/

Occasional Visitor

Re: How to access ALM data using REST API

I get a 403 error when I try to navigate to the Google Code link you posted with your java alm rest api example. Can you repost the link? 

Occasional Visitor

Re: How to access ALM data using REST API

I got 404 when i clicked teh google  code link. Could you please give the correct link ?

Occasional Visitor

Re: How to access ALM data using REST API

I get proxy errors and missing page messages. I thought this was HP's SUOPPORT site? Not very impressive work, gentlemen!

Acclaimed Contributor Acclaimed Contributor

Re: How to access ALM data using REST API

>I thought this was HP's SUPPORT site?

 

No, this is a peer to peer forum, not an official HP support site.

Trusted Contributor

Re: How to access ALM data using REST API

[ Edited ]

I've created a C# app that can authenticate, get and post to ALM REST API.

 

Should be very similar on Java.

 

Additionaly there are examples on JAVA in ALM REST API reference in ALM documant library.

 

Auth method:

 public void auth(string url, string xml)
        {          
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml.ToString());
                req.Method = "POST";
                req.ContentType = "application/xml";
                req.Accept = "application/xml";
                req.KeepAlive = true;
                req.AllowAutoRedirect = true;

                req.ContentLength = requestBytes.Length;
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
                backstr = sr.ReadToEnd();       
                myheader = res.Headers.Get("Set-Cookie"); 
                sr.Close();
                res.Close();

        }

 The tricky thing here that when you athenticate yourself you recieva a cookie that you SHOULD use further, when you do posts or gets.

But ALM returns you cookies NOT in http.cookie, but in HTTP response HEADER itself with name "Set-cookie".

So my header would be here: 

myheader = res.Headers.Get("Set-Cookie"); 

So now we call out auth method:

try
            {
// my creds, server domain etc are taken form apps UI.
                server_ = textBox2.Text;
                user_ = textBox5.Text;
                password_ = textBox6.Text;
                domain_ = textBox3.Text;
                project = textBox4.Text;
 
                REST.auth(server_ + "/authentication-point/alm-authenticate", "<?xml version='1.0' encoding='utf-8'?><alm-authentication><user>" + user_ + "</user><password>" + password_ + "</password></alm-authentication>");

                MessageBox.Show("Connected to REST");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 Here is get method:

 public void get(string url)
        {                             
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                req.Accept = "application/xml";
                req.KeepAlive = true;
                req.AllowAutoRedirect = true;
                req.Headers.Add("Cookie", myheader);

                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
                backstr = sr.ReadToEnd();               
                sr.Close();
                res.Close();
                        
        }

 So we add our header that we recieved after auth like : req.Headers.Add("Cookie", myheader);

cookie is stroed in myheader variable as we remember, right?

 

So here example of getting defects from ALM:

try
            {
               
// again creds, server, domain,etc. are taken from my apps variables.

                REST.get(server_ + "/rest/domains/" + domain_ +"/projects/" + project + "/defects/?page-size=1000");

                XPARSER.readXml(REST.backstr); // here I parse the response as I recieve xml - ignore that and everuthing that is below - it's relevant for my app.

               for (int i = 0; i < XPARSER.ids.Count; i++)
                {
                    listBox1.Items.Add(String.Format("{0} {1}", XPARSER.ids[i], XPARSER.names[i]));
                }
                XPARSER.ids.Clear();
                XPARSER.names.Clear();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 Here is post method:

public void post(string url, string xml)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml.ToString());
            req.Method = "POST";
            req.ContentType = "application/xml";
            req.Accept = "application/xml";
            req.KeepAlive = true;
            req.AllowAutoRedirect = true;
            req.Headers.Add("Cookie", myheader);

            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();
        }

When you want to create something you should send post http request.

Along with url you should provide an XML of an entity you are tying to post.

If you don't know how xml should look like - do a get request first

- recieve an xml and see how it looks like.

Here is example of posting a defect:

 try 
            {
                
                for (int i = 0; i < rest_bCount; ++i)
                   {

                      
                    REST.post(server_ + "/rest/domains/" + domain_ + "/projects/" + project + "/defects/",
                        @"<?xml version='1.0' encoding='utf-8'?>
                        <Entity Type='defect'>
                        <Fields>
                        <Field Name='description'>
                        <Value>desc</Value>
                        </Field>
                        <Field Name='name'>
                        <Value>defect created from rest" + i + Convert.ToString(rand.Next()) + "</Value></Field><Field Name='status'><Value>" + arr[i % arr.Length] + "</Value></Field><Field Name='severity'><Value>" + arr2[i % arr2.Length] + "</Value></Field><Field Name='detected-by'><Value>" + user_ + "</Value></Field><Field Name='creation-time'><Value>" + date.ToString(format) + "</Value></Field></Fields></Entity>"
                        );
                    XPARSER.readXml(REST.backstr);
}
 catch (Exception ex)
                {
                   
                    MessageBox.Show(ex.Message);
                }

 Hope that will help you a bit.

Advisor

Re: How to access ALM data using REST API

Great post on adding the defect.  My problem is how do I add a new test run and have it update all the necessary locations?

 

I've actually been able to add a run under the Test Instance Details but it doesn't update the Test on the Execution Grid.  I'd like for it to update that Status, Exec Date, Exec Time, etc.  Does anyone know what is actually needed to update this?

 

Occasional Visitor

Re: How to access ALM data using REST API

Hi  ,

 

   Thanks for the sample .Net code I have used the same but always I am getting "The remote server returned an error: (401) Unauthorized" exception (the test user iD /PWD will work perfect when logging in to QC manually). Can you please suggest is there anything need to setup in QC or any suggestions will be greatly appreciated.

 

Thanks in Advance

 

Valued Contributor

Re: How to access ALM data using REST API

There are different ways (3 i guess) to authenticate into QC using REST. I'm using basic authentication and never tried web authentication as above. The basic concept is that when you authorize, a token cookie will be returned. Use this token cookie to perform an operation, which will return a session cookie and you need to use this session cookie on subsequent operations. On each operation a new session cookie will be returned. This is how session is managed in QC while using REST.

See if cookies are properly set.
Occasional Visitor

Re: How to access ALM data using REST API

 HI,

 

Is there a way where we can create a test set using the HP ALM Rest API.

 

Also executing the Rest APi using rest client in firefox. Im getting the following error msg

 

  1. Status Code: 401 Authentication failed. Browser based integrations - to login append '?login-form-required=y to the url you tried to access.
  2. Cache-Control: must-revalidate,no-cache,no-store
  3. Content-Length: 1662
  4. Content-Type: text/html;charset=ISO-8859-1
  5. Date: Mon, 07 Apr 2014 19:10:34 GMT
  6. Server: Jetty(7.5.4.v20111024)
  7. WWW-Authenticate: LWSSO realm=http://XXXXXXXXXX/qcbin/authentication-point

 

please suggest!!

 

Thanks

Keerthi.


Pradeep0905 wrote:
There are different ways (3 i guess) to authenticate into QC using REST. I'm using basic authentication and never tried web authentication as above. The basic concept is that when you authorize, a token cookie will be returned. Use this token cookie to perform an operation, which will return a session cookie and you need to use this session cookie on subsequent operations. On each operation a new session cookie will be returned. This is how session is managed in QC while using REST.

See if cookies are properly set.

 

Occasional Visitor

Re: How to access ALM data using REST API

Hi All,

 

Does the given ALM REST API works with LAB Management as well? I am trying to generate a report on hourly basis to get the list of timelots and active test runs from lab management portal. Can i achive this via ALM REST API? or Is there any other way to generate such a report? Any help/pointers will be really helpful.

 

Thanks.

 

 

Frequent Visitor

Re: How to access ALM data using REST API

Hi,

 

Thanks for you post.

I used your code for Authenticating and getting resources by GET request. all are working. But I am facing issue with POST reqeust.

 

I have used the same code for create run, but it is only giving 401 unauthorized error. I added the two cookies as they mentioned in the document for POST request for creating new entities. I tried with one cookie(Lwsso) also, it is not working.

Please help me if anything to do with the cookie.

 

Please see the code below I have used.

 

Thansk in advance,

Ragukumar.

 

HttpWebRequest req = ("HttpWebRequest)WebRequest.Create(http://server-name/qcbin/rest/domains/domain-name/projects/project-name/runs");

            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml.ToString())​;
            req.Method = "POST";
            req.ContentType = "application/xml";
            req.Accept = "application/xml";
            req.KeepAlive = true;
            req.AllowAutoRedirect = true;
            req.Headers.Add("Cookie", myheader1);//QCSession cookie

            req.Headers.Add("Cookie", myheade2r);//Lwsso Key cookie

            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();

 

 

Occasional Visitor

Re: How to access ALM data using REST API

Hi,

 

Thanks you post.

I have implemented code in C#(ALM API) and its working.

i am able to login and getting all defect list using my ALM credential but after getting defect details connection is not getting close automatically. It remain on open on server.

Please let me know if any api url which disconnect or kill session immediate after retrieving defect?

 

Thanks

Sam

Occasional Visitor

Re: How to access ALM data using REST API

Hi,

 

i am also looking for same, but given link is not working, can you please help.

Occasional Visitor

Re: How to access ALM data using REST API

[ Edited ]

I would like to know if the following tasks are possible with Rest API For ALM 11?

Task I will need to do:
Authenticate to server: (DONE)


Access a test run:
Read test run status and type.

Write to test run test:
Set to Manual on Fail
update result to pass fail
attach test report .zip file to test in run.

I am struggling with the classifications:


I can connect and authenticate and read back the tests created in the testing section,
I can get data with various other keywords I have gleaned from the forums.

How do I get data for a specific 'test-set/test lab/excecution-grid/' in a manner that will allow me to interogate them?

args = "test-folders" # for test definitions ?
args = "test-sets" # for tests in run?
args = "tests" ?
args = "runs" ?

http://<host>:<port>/qcbin/rest/domains/<domain>/projects/<project>/<args>


I have a project image  (Capture.PNG) attached and would like specifically to interact with "Dummy Run" for proof of concept.