Quality Center / ALM Practitioners Forum
cancel

Cannot access any entity in ALM 12.2 via REST

MarioRossi
Occasional Contributor

Cannot access any entity in ALM 12.2 via REST

Hello gurus,

I am trying to access via REST a defect for example, but I am getting an error.

Here is my simple code:

try {

URL url = new URL("http://192.168.136.133:8080/qcbin/rest/domains/DEFAULT/projects/test/defects/1");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/xml");


byte[] credBytes = ("admin" + ":" + "admin").getBytes();
String credEncodedString = "Basic " + Base64Encoder.encode(credBytes);

System.out.println(credEncodedString);

conn.setRequestProperty ("Authorization", credEncodedString);

System.out.println(conn.getHeaderFields());

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();:

}

 

This is what I am getting:

Basic YWRtaW46YWRtaW4=
{null=[HTTP/1.1 401 Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.], WWW-Authenticate=[LWSSO realm="http://192.168.136.133:8080/qcbin/authentication-point"], Content-Length=[564], Set-Cookie=[JSESSIONID=15jk915ojntqz103yz016ekjfi;Path=/qcbin;HttpOnly], Content-Type=[text/html; charset=ISO-8859-1], Server=[Jetty(9.1.4.v20140401)], Cache-Control=[must-revalidate,no-cache,no-store]}
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 401
at WSDemo.main(WSDemo.java:127)
Java Result: 1

 

Any Idea?

Many thanks!

 

Regards,

Mario

 

11 REPLIES
leobor
Occasional Visitor

Re: Cannot access any entity in ALM 12.2 via REST

You need to authenticate firsr - take a look at the example in REST API help

Jan Czajkowski
Honored Contributor

Re: Cannot access any entity in ALM 12.2 via REST

I don't know about the code details, but I know that you need to authenticate and get hold of the session cookie before you can go straight to the defect.

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
MarioRossi
Occasional Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Actually the code comes more or less from the documentation.

I tried to run ReadExample but I get the followinf error:

Exception in thread "main" java.lang.RuntimeException: failed obtaining response for http://192.168.136.133:8080/qcbin/rest/server
at Assert.assertEquals(Assert.java:20)
at ReadExample.readExample(ReadExample.java:57)
at ReadExample.main(ReadExample.java:15)
Java Result: 1

The I found something on the HP forum suggesting that something changed between alm 11 and 12 and the documentation needs to be updated with this code that needs to be added in RestConnector class and call it before requesting a resource:

 public void getQCSession(){
        String qcsessionurl = this.buildUrl("rest/site-session");
        Map<String, String> requestHeaders = new HashMap<String, String>();
        requestHeaders.put("Content-Type", "application/xml");
        requestHeaders.put("Accept", "application/xml");
        try {
            Response resp = this.httpPost(qcsessionurl, null, requestHeaders);
            this.updateCookies(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }

 

But using this method in the code I get another error:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://192.168.136.133:8080/qcbin/rest/domains/DEFAULT/projects/Test/defects
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1676)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1672)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
at org.hp.qc.web.restapi.docexamples.docexamples.infrastructure.RestConnector.retrieveHtmlResponse(RestConnector.java:242)
at org.hp.qc.web.restapi.docexamples.docexamples.infrastructure.RestConnector.doHttp(RestConnector.java:154)
at org.hp.qc.web.restapi.docexamples.docexamples.infrastructure.RestConnector.httpPost(RestConnector.java:103)
at CreateDeleteExample.createEntity(CreateDeleteExample.java:120)
at ReadExample.readExample(ReadExample.java:86)
at ReadExample.main(ReadExample.java:15)
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://192.168.136.133:8080/qcbin/rest/domains/DEFAULT/projects/Test/defects
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at org.hp.qc.web.restapi.docexamples.docexamples.infrastructure.RestConnector.retrieveHtmlResponse(RestConnector.java:236)
... 5 more
Java Result: 1

 

Any idea?

 

Many thanks guys!

 

ameyjo
HPE Expert

Re: Cannot access any entity in ALM 12.2 via REST

What is the response of this URL http://<ALMServer>:<Port>/qcbin/rest/domains/<Domain>/projects/<project name>/defects if you paste it in your browser?  does it returns something?

Cordially

 

"HP Support
If you find this or any post resolves your issue, please be sure to mark it as an accepted solution"
ALMPR
Acclaimed Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Hi,

I am also trying to access entities in ALM 12 via REST and facing the same problem. I got the user Authenticated and manage the session using QC Session cookie.

After this, I am trying to access domain list /project list  which is not working

I am trying to access Tests/Defects - which is not working.

When I paste  http://<ALMServer>:<Port>/qcbin/rest/domains/<Domain>/projects/<project name>/defects in URL, I am getting the ERROR 401: Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access.

The same error when I tried to access domains/projects/Tests as well

Regards

ALM User : Pras

 

 

ALMPR
Acclaimed Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Hi,

I am able to retrieve the details of domains/projects/tests .

Regards

ALM User : Pras

 

 

arn-arn
Acclaimed Contributor

Re: Cannot access any entity in ALM 12.2 via REST

after adding and calling the below code, i was able to connect and get the resource.

public void getQCSession(){

String qcsessionurl = this.buildUrl("rest/site-session");
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Content-Type", "application/xml");
requestHeaders.put("Accept", "application/xml");
try {
Response resp = this.httpPost(qcsessionurl, null, requestHeaders);
this.updateCookies(resp);
} catch (Exception e) {
e.printStackTrace();
}
}

Han_Solo
Occasional Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Where exactly did you put this method?  

"after adding and calling the below code, I was able to get the resource."

Where did you add it and how did you call it?

Ibrahimsha
Super Collector

Re: Cannot access any entity in ALM 12.2 via REST

Below is the simple Excel macro code to access ALM Data using REST API. Hope it helps!!

Sub Test_REST_API()

Dim item, ServerName, sUrl, QCCookie, LWSSOCookie
Dim reqAddr As String
Dim WinHttpReq As WinHttp.WinHttpRequest
Set WinHttpReq = New WinHttpRequest

sUrl = "https://almservername/qcbin"
ServerName = sUrl + "/authentication-point/alm-authenticate"

WinHttpReq.Open "POST", ServerName, False
WinHttpReq.setRequestHeader "Content-type", "application/xml"
WinHttpReq.setRequestHeader "Accept", "application/xml"
WinHttpReq.send ("<alm-authentication><user>username</user><password>password</password></alm-authentication>")

If WinHttpReq.status = 200 Then
item = WinHttpReq.getAllResponseHeaders
LWSSOCookie = CStr(Right(item, Len(item) - (InStrRev(item, "LWSSO_COOKIE_KEY") - 1)))
End If

'*********************************************************************************

reqAddr = sUrl + "/rest/site-session"

WinHttpReq.Open "POST", reqAddr, False
WinHttpReq.setRequestHeader "Content-type", "application/xml"
WinHttpReq.setRequestHeader "Accept", "application/xml"
WinHttpReq.setRequestHeader "Cookie", LWSSOCookie
WinHttpReq.send ("<session-parameters><client-type>REST Client</client-type></session-parameters>")

If WinHttpReq.status = 201 Then
item = WinHttpReq.getAllResponseHeaders
QCCookie = CStr(Right(item, Len(item) - (InStrRev(item, "QCSession") - 1)))
End If

'*********************************************************************************
'Following code reads the domain list
'Dim WinHttpReq As WinHttp.WinHttpRequest
'Set WinHttpReq = New WinHttpRequest

reqAddr = sUrl + "/rest/domains"

WinHttpReq.Open "GET", reqAddr, False
WinHttpReq.setRequestHeader "Content-type", "application/xml"
WinHttpReq.setRequestHeader "Accept", "application/xml"
WinHttpReq.setRequestHeader "Cookie", LWSSOCookie
WinHttpReq.setRequestHeader "Cookie", QCCookie
WinHttpReq.send
If WinHttpReq.status = 200 Then
item = WinHttpReq.getAllResponseHeaders
strRes = WinHttpReq.responseText
End If

MsgBox strRes

'**********************************************************************************

End Sub

 

 

Restfreak
Acclaimed Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Hi , 

I am trying integrate SOAPUi with ALM12.0 using REST API. During this I am able to authenticate to ALM and create session. after creating session. I am able to get all session cookies.

Set-Cookie: JSESSIONID=xxxxxxxxxxxxxxxxxx;Path=/qcbin;HttpOnly
Set-Cookie: QCSession=XXXXXXXx;Path=/;HTTPOnly
Set-Cookie: ALM_USER=xxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/
Set-Cookie: XSRF-TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/

Now while i try to GET the testset by test set Id.

/qcbin/rest/domains/{domain}/projects/{project}/test-sets/{Test Set ID}

I am passing X-XSRF Token, Domain,project and Test set Id, getting a response "Authentication failed. Browser based integrations - to login append '?login-form-required=y to the url you tried to access."

 I am performing these REST calls using SOAPUI pro. I am not able to understand what is going wrong here? Are there any setting need to be changed within SOAP UI/ Browser?

I don't want to use ReadyAPI for SOAPUI to ALM integration. 

Thanks 

RestFreak

 

 

Restfreak
Acclaimed Contributor

Re: Cannot access any entity in ALM 12.2 via REST

Any who has encountered same issue? Anyone got solution Please let me know, It's bit urgent.

Thanks 

RestFreak

//Add this to "OnDomLoad" event