简体   繁体   中英

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

Setup: Python 2.7.10, Requests library, Windows 8.1

I am new to json-rpc and need to set up automation tests for sending POST requests and checking the response. I am having difficulty accessing the data after logging in. I need to log in to the system first with a POST request ("method": "identity.authenticate"), and then the test after that is also a POST request that should return data after I have been logged in.

The developers also gave me a Bearer token to pass in the Headers which they claimed should work across all tests, and it did seem to work when I tried it in Postman (although I did have some mixed results), but for some reason when I try to use it with my Python Requests set up, it returns an error every time. The code is below (have to change the working data to dummy data, so trying to run the below code as-is won't work, but hopefully seeing the set up can help to pinpoint what I may be doing wrong):

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)

I've tried many variations, such as not passing the Bearer token in the headers, only passing the Bearer token and not the signin payload, and nothing has worked so far.

When I run the above code with non-dummy data, this is the console output (with my added comments for clarity):

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

The issue is the error with the "module not found" response, where it should be returning relevant data for that request.

On non-rpc HTTPS sites I've used POST data to log into a site as I am doing above, and then ran through a for loop of URLs as GET requests to check data is being returned properly in that logged in site, so I'm not sure if it's because I have to send POST requests here to retrieve the test responses. If anybody has any helpful hints, I would greatly appreciate it.

I finally found out the issue was due to a recent URL change in the API that I was not informed about, which explains why the URL originally worked in Postman.

To make it more confusing, the URL for only some of the methods were changed, which is why the login identity.authenticate POST worked, as it was still using the original URL. For the other POST commands, the methods/modules were no longer present with the original URL, hence the error message "module not found."

Once I updated the base_url variable with the new base_url for the payloads that were receiving the error message, they returned the proper data.

Hopefully this can help others, in the event they run into something similar.

To sum up: the code works fine, it was the data that was being input into the code that was flawed and why the error message was being received. In this case, the data that was flawed was the base_url variable was pointing to an outdated URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM