How to access ALM data using REST API (5087 Views)
Reply
Occasional Advisor
umapendse
Posts: 17
Registered: ‎09-27-2010
Message 1 of 15 (5,087 Views)

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.
Please use plain text.
Frequent Advisor
Elad Avraham
Posts: 40
Registered: ‎12-06-2010
Message 2 of 15 (5,087 Views)

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
Please use plain text.
Occasional Contributor
Justin Zhang
Posts: 5
Registered: ‎06-14-2011
Message 3 of 15 (5,087 Views)

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?
Please use plain text.
Occasional Contributor
Justin Zhang
Posts: 5
Registered: ‎06-14-2011
Message 4 of 15 (5,087 Views)

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...

Please use plain text.
Occasional Visitor
hugelsb
Posts: 2
Registered: ‎12-01-2011
Message 5 of 15 (4,873 Views)

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.

Please use plain text.
Occasional Visitor
petejim
Posts: 1
Registered: ‎02-07-2012
Message 6 of 15 (4,670 Views)

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.

Please use plain text.
Occasional Visitor
hugelsb
Posts: 2
Registered: ‎12-01-2011
Message 7 of 15 (4,647 Views)

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.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 23,109
Registered: ‎03-06-2006
Message 8 of 15 (4,621 Views)

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...

Please use plain text.
Established Member
MelodyG
Posts: 2
Registered: ‎04-24-2012
Message 9 of 15 (4,245 Views)

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. "

Please use plain text.
Occasional Visitor
gonella
Posts: 1
Registered: ‎08-31-2012
Message 10 of 15 (3,185 Views)

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/

Please use plain text.
Occasional Visitor
AtlMike
Posts: 1
Registered: ‎10-30-2012
Message 11 of 15 (2,667 Views)

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? 

Please use plain text.
Occasional Visitor
RameshBadam
Posts: 1
Registered: ‎01-29-2013
Message 12 of 15 (1,860 Views)

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 ?

Please use plain text.
Occasional Visitor
Splendiferous
Posts: 1
Registered: ‎10-25-2012
Message 13 of 15 (1,549 Views)

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!

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 23,109
Registered: ‎03-06-2006
Message 14 of 15 (1,466 Views)

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.

Please use plain text.
Valued Contributor
alexanderk
Posts: 83
Registered: ‎04-03-2012
Message 15 of 15 (1,443 Views)

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.

Please use plain text.