本文介绍了RallyDev:无法创建缺陷.服务器说“无法解析输入...";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原帖:我正在尝试以编程方式创建缺陷.我遇到了一些错误并且无法再进一步.这里基本上是代码:

Original Post:I'm trying to create defects programmatically. I am getting a couple errors and having trouble getting any further. Here, essentially, is the code:

import requests, json

rally_auth = ('my_user', 'my_pw')
rally_auth_url = 'https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize'
rally_defect = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/defect'
workspace_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345'
fe_project_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/project/7890'
current_fe_release_ref = "https://rally1.rallydev.com/slm/webservice/v2.0/release/45678"

r = requests.get(rally_auth_url, auth=rally_auth)
token = r.json()['OperationResult']['SecurityToken']
url = rally_defect + '/create?key=' + token

payload = {
  'Name': 'My defect',
  'State': 'Open',
  'Project': fe_project_ref,
  'Rank': 120,
  'Release': current_fe_release_ref,
  'key': token
}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), auth=rally_auth, headers=headers)

您会注意到我已将令牌放入 POST 的 URL 和数据中.API 文档说我应该在 URL 中包含密钥,但是如果我在 POST 数据中不包含密钥,我会得到:

You'll notice that i've put the token in both the POST's URL and data. The API docs say that I should have the key in the URL, but if I do not include the key in the POST data I get:

{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Not authorized to perform action: Invalid key"], "Warnings": []}}

如果我确实包含密钥,API 请求会以不同的方式失败.它将在第一个逗号处失败.

If I do include the key, the API request falls over differently. It will fail on the first comma.

{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Cannot parse input stream due to I/O error as JSON document: Parse error: expected '}' but saw ',' [ chars read = >>>{\"Name\": \"My defect\",<<< ]"], "Warnings": []}}

我很困惑.

固定代码感谢@nickm

import requests, json

rally_auth = ('my_user', 'my_pw')
rally_auth_url = 'https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize'
rally_defect = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/defect'
workspace_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345'
fe_project_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/project/7890'
current_fe_release_ref = "https://rally1.rallydev.com/slm/webservice/v2.0/release/45678"

s = requests.Session()
r = s.get(rally_auth_url, auth=rally_auth)
token = r.json()['OperationResult']['SecurityToken']
url = rally_defect + '/create?key=' + token

payload = {
  'Name': 'My defect',
  'State': 'Open',
  'Project': fe_project_ref,
  'Rank': 120,
  'Release': current_fe_release_ref,
}
headers = {'content-type': 'application/json'}
r = s.post(url, data=json.dumps(payload), headers=headers)

推荐答案

如果您使用的是 WS API v2.0,则更新和创建请求需要令牌,因此您将其包含在您的 post 请求 url 中是正确的.

If you are using v2.0 of WS API, the token is required for update and create requests, so you are correct by including it in your post request url.

如果令牌未附加到请求或令牌对于特定会话无效,则会出现无效密钥错误.当直接访问端点时,我们必须使用 cookie 维护 http 会话,否则 post 发生在新会话的上下文中 - 与我们获得令牌的会话不同.

The invalid key error will come up if a token is not appended to the request or if the token is invalid for a specific session. When hitting the endpoints directly we have to maintain an http session with a cookie, otherwise the post happens in the context of a new session - different from the one in which we got the token.

请参阅 这篇文章.它并不特定于 Python,但在概念上是相同的.

Please see this post. It is not specific to Python, but conceptually it's the same.

我注意到负载中的 Rank.你有排名自定义字段吗?Rally 的 v2.0 中没有这样的内置字段.有一个 DragAndDropRank,它不是数字,通过提供 120 的值来设置它是行不通的.此外,您是否在有效负载中尝试过双引号而不是单引号?

I noticed Rank in the payload. Do you have a Rank custom field? There is no such built-in field in v2.0 in Rally. There is a DragAndDropRank, which is not numeric, and setting it by supplying a value of 120 will not work.Also, did you try double quotes instead of single quotes in the payload?

有一个 pyral - Rally Python 工具包,它提供了方便的方法,因此您没有直接命中端点.目前它适用于 1.43 的 WS API.该工具包不受官方支持,但我希望它会在 2014 年 6 月之前更新以与 WS API 的 v2.0 一起使用,届时 1.43 不再受支持(per 此时间表).安全令牌是在 v2.0 中引入的.WS API 1.43 中不存在用于发布请求的额外身份验证层,如果使用 pyral,则无需处理令牌.

There is a pyral - Rally Python toolkit that provide convenience methods so you do not have to hit endpoints directly. Currently it works with 1.43 of WS API. The toolkit is not officially supported, but I expect that it will be updated to work with v2.0 of WS API before June 2014, when 1.43 is no longer supported (per this schedule).Security token was introduced in v2.0. This extra authentication layer for post requests does not exist in 1.43 of WS API, and you do not have to deal with the token if using pyral.

这篇关于RallyDev:无法创建缺陷.服务器说“无法解析输入...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:49