2017-07-01 106 views
0

设置正确的POST数据响应:Python的2.7.10,请求库时,Windows 8.1JSON-RPC - 无法检索从登录网站与Python库请求

我是新来的JSON-RPC和需要设置自动测试发送POST请求并检查响应。我在登录后无法访问数据。我需要首先使用POST请求(“method”:“identity.authenticate”)登录系统,然后之后的测试也是一个POST请求,应该返回我已经登录后的数据。

开发人员还给了我一个不记名标记来传递他们声称应该在所有测试中工作的标题,而且当我在Postman中尝试时,它确实有效(尽管我做了有一些混合的结果),但由于某种原因,当我尝试将它与我的Python请求设置一起使用时,它每次都会返回一个错误。代码如下(必须将工作数据更改为虚拟数据,因此尝试按原样运行下面的代码将不起作用,但希望看到设置可以帮助查明我可能做错了什么):

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) 

我已经尝试了许多变化,如不经过在头承载令牌,则只有通过承载令牌,而不是登入有效载荷,迄今没有奏效。

当我运行与非虚拟数据上面的代码,这是控制台输出(与我为清晰起见添加了注释):

# 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'} 

的问题与错误“模块未找到”响应,它应该返回该请求的相关数据。

在非rpc HTTPS网站我已经使用POST数据登录到一个站点,因为我在上面做了,然后通过一个for循环的URL作为GET请求来检查数据被正确返回登录网站,所以我不确定是否因为我必须在这里发送POST请求来检索测试响应。如果有人有任何有用的提示,我将不胜感激。

回答

0

我终于发现这个问题是由于API中最近一次URL更改导致我没有通知,这就解释了为什么这个URL最初在Postman中工作。

为了让它更混乱,只改变了一些方法的URL,这就是为什么登录身份验证POST工作,因为它仍然使用原始URL。对于其他POST命令,方法/模块不再与原始URL一起出现,因此出现错误消息“找不到模块”。

一旦我更新base_url变量与新的base_url接收到错误消息的有效内容,他们返回了正确的数据。

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

总结:代码工作正常,它是输入到有缺陷的代码中的数据以及为什么收到错误消息。在这种情况下,有瑕疵的数据是base_url变量指向过时的URL。