2

I am automating alm with rest api and python . All i could do now is to login and logout.Only apis for login and logout gives a proper response.All other is giving either 500/401 error.

import requests
from requests.auth import HTTPBasicAuth
from xml.etree.ElementTree import Element, SubElement, tostring, parse
ALM_USER_NAME = "XXXX"
ALM_PASSWORD = "XXXX"
ALM_DOMAIN = "XXXX"
ALM_PROJECT="XXXX"

ALM_URL ="http://xxxxxxxxx/qcbin/"
QC_LOGOUT_END_POINT = ALM_URL + "authentication-point/logout"
session = requests.Session()
session.verify = False

auth = session.post(ALM_URL + "authentication-point/authenticate",
                    auth=HTTPBasicAuth(ALM_USER_NAME, ALM_PASSWORD))
print("Authentication ", auth, auth.text, session.cookies)

site_session = session.post(ALM_URL + "rest/site-session")
print("Session ", site_session, site_session.text, session.cookies)

check = session.get(ALM_URL + "rest/is-authenticated")
print("Check ", check, check.text)

# Enforce JSON output
session.headers.update({ 'Accept': 'application/json','Cookie': check.headers['set-cookie']})
#projects = session.get(hpqc_server + "rest/domains/"+ALM_DOMAIN+"/projects")
# Post a New Defect
defect = dict()
defect['Type'] = 'defect'
defect['name'] = 'StackOverflow'
defect['user-10'] = 'Content'  # User defined field
defect['severity'] = '1-Low'
defect['priority'] = '1-Low'
defect['detected-by'] = 'userid'
defect['creation-time'] = '2017-11-13'

# Call the generic method to build the xml data
defect_payload = generate_xml_data(defect)

deft=session.get(ALM_URL + "rest/domains/"+ALM_DOMAIN+"/projects/"+ALM_PROJECT+"/content/defects")
print(deft.status_code)


if session:
            res = session.post(QC_LOGOUT_END_POINT)
            if res.status_code == 200:
                print ("ALM: Logged out")

response for login:200 response for session:201 response for defects:401

  • I'm running into the same issue when using Python even though I can get it to work without a problem in Postman – Robert Feb 11 '20 at 21:15

2 Answers2

1

Looks like your request lacks cookies. Please debug it/print out all cookies. In my case (also using python requests API with ALM 12.55) the following cookies are sent with each request (e.g. to read defects):

ALM_USER
LWSSO_COOKIE_KEY
QCSession
XSRF-TOKEN
JSESSIONID

Summary of required steps:

  1. Post an authentication request to authentication-point/authenticate. Save the returned LWSSO_COOKIE_KEY cookie.
  2. Post a session request to rest/site-session and send the LWSSO_COOKIE_KEY cookie with it. Save the returned ALM_USER, QCSession, XSRF-TOKEN and JSESSIONID cookies
  3. Merge all cookies into one cookie container (RequestsCookieJar.update(self, other))

In your "# Enforce JSON output" line you update the cookies in the header. You set the cookies of check. Maybe the cookie of site_session is missing.

Marteng
  • 1,179
  • 13
  • 31
  • 1
    I wish I saw this message earlier. You are correct, although apparently only the QCSession and LWSSO_COOKIE_KEY are required. The other cookies are not. – Robert Feb 12 '20 at 15:19
0

I managed to get it working. I needed to make the initial call to the sign in URL to get the cookies for LWSSO_COOKIE_KEY and QCSession. After this, i'm getting the response i needed.

Here's my code with the url and password slightly changed for security reasons. Also, I got the encrypted authorization Basic password from looking at the python code generated by Postman:

url_signin = "http://????????????/qcbin/api/authentication/sign-in"
url_defects = "http://??????????/qcbin/api/domains/{Domain name}/projects/{project_name}/defects"
payload  = {}

headers1 = {
    "Authorization": "Basic ?????????"}

si = requests.get(url_signin, headers = headers1)
QCsession = si.cookies.items()[2][1]
lwsso = si.cookies.items()[1][1]

headers = {
    "Authorization": "Basic ??????????",
    'Cookie': "QCSession="+ QCsession + "; "+"LWSSO_COOKIE_KEY=" +lwsso
}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text)

Let me know if you have any questions

Robert
  • 183
  • 2
  • 13