HP ALM Python rest api-如何为API调用正确传递cookie

时间:2018-07-28 22:23:32

标签: python rest hp-alm

如何为其余api调用传递HP ALM身份验证会话对象。我正在跟踪一些示例,以通过REST API连接到HP ALM,以运行基本的CRUD操作。

例子之一-https://github.com/vkosuri/py-hpalm/blob/master/hpalm/hpalm.py

下面是建立连接的代码片段,该连接工作正常。我收到200 OK登录响应。

headers = {'Cookie' : lwssocookie}
headers["Accept"] = 'application/xml'
login_url = self.base_url + '/qcbin/authentication-point/authenticate'

resp = requests.get(login_url, headers=headers, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify)
alm_session = resp.headers['Set-Cookie']
logger.debug("Is QC session launched: %s" %alm_session)
cookie = ";".join((lwssocookie, alm_session))

但是,即使我将Cookie添加到标头中,所有后续操作都失败,并出现未经授权的错误

self.headers['cookie'] = cookie
url = self.base_url + '/qcbin/rest/domains/' + self.domain + '/projects/' + self.project + '/test-instances'
response = requests.get(url, params=params, headers=self.getheaders())

任何人都可以建议如何举行会话以运行操作以及我在这里缺少什么吗?

我还试图按如下所示在get调用中传递cookie,即使那样也不起作用。

response = requests.get(url, params=params, headers=self.getheaders(), cookies=cookie)

提前谢谢

1 个答案:

答案 0 :(得分:1)

感谢requests.Session(),无需为Cookie操纵HTTP标头。

这是我针对旧版ALM 12.01运行的代码段,由于this post,我花了几个小时来设计:

session = requests.Session()
session.verify = False

auth = session.post(hpqc_server + "authentication-point/authenticate?login-form-required=y",
                    auth=HTTPBasicAuth(username, password))
print("Authentication ", auth, auth.text, session.cookies)

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

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

# Enforce JSON output
session.headers.update({ 'Accept': 'application/json' })
projects = session.get(hpqc_server + "rest/domains/DOMAIN/projects")