JSON-RPC - 无法从使用 Python 请求库的已登录站点检索正确的 POST 数据响应

JSON-RPC - Cannot retrieve correct POST data response from a logged in site with Python Requests library

设置:Python 2.7.10,请求库,Windows 8.1

我是 json-rpc 的新手,需要设置自动化测试以发送 POST 请求并检查响应。登录后访问数据有困难,需要先用POST请求("method":"identity.authenticate")登录系统,然后再测试也是一个 POST 请求,在我登录后应该 return 数据。

开发人员还给了我一个 Bearer 令牌,用于传递 Headers 他们声称应该适用于所有测试,当我在 Postman 中尝试时它似乎确实有效(尽管我确实有一些混合结果),但出于某种原因,当我尝试将它与我的 Python 请求设置一起使用时,它每次都会 return 出错。代码如下(必须将工作数据更改为虚拟数据,因此尝试 运行 下面的代码 as-is 将不起作用,但希望看到设置可以帮助查明我可能是什么做错了):

import requests
from pprint import pprint

base_url = "https://rpc_url.com/rpc"
custom_headers = {"Content-Type":"application/json", "Authorization":"Bearer token"}

'''Sign in payload'''
signinPayload = {"method" : "identity.authenticate",
"id" : 1,
"jsonrpc" : "2.0",
"params" : {"password" : "password", "username" : "username"}}

test1_Payload = {"jsonrpc" : "2.0",
"method" : "fc.pick.getPendingPicks",
"id" : 20, "params" :{"_tags" :{"device" : "device",
  "deviceOS" : "deviceOS", "firstName" : "firstName",
  "devicetype" : "devicetype", "appVersion" : "appVersion",
  "login" : "username", "devicelabel" : "devicelabel",
  "lastName" : "lastName"}}}

with requests.Session() as s:
    # below passes the login payload, which returns proper data.
    login_post = s.post(base_url, json=signinPayload, headers=custom_headers).json()
    pprint(login_post)
    ###########################
    # passes the test payload, which doesn't work correctly
    r = s.post(base_url, json=test1_Payload, headers=custom_headers).json()
    pprint(r)

我尝试了很多变体,例如不在 headers 中传递 Bearer 令牌,只传递 Bearer 令牌而不传递登录负载,但到目前为止没有任何效果。

当我 运行 上面的代码和 non-dummy 数据时,这是控制台输出(为清楚起见我添加了注释):

# Below is returned for sign in authenticate, which appears to be good data
{u'fcFlingVersion': u'1.0.9',
    u'id': 1, u'jsonrpc': 
    u'2.0', 
    u'result': {
         u'_idp': {u'accessToken': 'accessTokenNumber'},
         u'administratorId': 1,
         u'changePassword': False,
         u'companyId': 1,
         u'daysUntilPasswordExpires': 100,
         u'firstname': u'firstName',
         u'groupList': u'groupList',
         u'lastname': u'lastName',
         u'login': u'username',
         u'passwordDirectory': u'identity',
         u'photoUrl': u'https://photoURL',
         u'roles': [],
         u'userId': 1,
         u'username': u'username'}}

# Below is the returned error data for test1_Payload after the successful sign in test from above.
{u'error': {u'code': 404, u'data': None, u'message': u'module not found'},
 u'fcFlingVersion': u'1.0.9',
 u'id': 20,
 u'jsonrpc': u'2.0'}

问题是 "module not found" 响应错误,应该是 returning 该请求的相关数据。

在 non-rpc HTTPS 站点上,我使用 POST 数据登录站点,就像我在上面所做的那样,然后 运行 通过 URL 的 for 循环作为 GET 请求检查数据是否在该登录站点中正确 returned,所以我不确定是否因为我必须在此处发送 POST 请求以检索测试响应。如果有人有任何有用的提示,我将不胜感激。

我终于发现这个问题是由于 API 最近的 URL 变化,我没有被告知,这解释了为什么 URL 最初在 Postman 中工作。

为了让它更加混乱,仅更改了部分方法的 URL,这就是登录 identity.authenticate POST 起作用的原因,因为它仍在使用原始方法URL。对于其他 POST 命令,methods/modules 不再与原始 URL 一起出现,因此出现错误消息 "module not found."

一旦我用新的 base_url 更新了 base_url 变量,用于接收错误消息的负载,它们返回了正确的数据。

希望这可以帮助其他人,如果他们 运行 遇到类似的事情。

总而言之:代码工作正常,是输入到代码中的数据有缺陷,以及收到错误消息的原因。在这种情况下,有缺陷的数据是 base_url 变量指向过时的 URL.