python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > requests处理token鉴权接口和jsonpath使用

requests处理token鉴权接口和jsonpath使用方式

作者:葬爱家族小阿杰

文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达式来提取json数据,包括匹配单个值、多个值、通过索引和条件过滤等,通过实际例子和语法说明,帮助读者更好地理解和应用jsonpath工具

requests处理token鉴权接口和jsonpath使用

第一步:登陆,提取token保存起来

# 登陆接口的地址
url = 'http://www.adf.com/futureloan/member/login'
# 登陆接口的参数
params = {
    "mobile_phone": "123456789",
    "pwd": "123456",
}
# 请求头
headers = {"X-Media-Type": "DuSSS.v1"}
# 发送请求进行登陆
response = requests.post(url=url, headers=headers, json=params)
res = response.json()
token = res['data']['token_info']['token']

第二步:访问需要鉴权的接口时,带上token就可以了

# 提取登陆用户的id
member_id = res['data']['id']
params2 = {
    "member_id": member_id,
    "amount": "2000",
}
# 充值接口的地址
url2 = 'http://www.adf.com/futureloan/member/recharge'
# 请求头
headers2 = {"X-Media-Type": "DuSSS.v2",
           "Authorization": "Bearer " + token}
response2 = requests.post(url=url2, headers=headers2, json=params2)
response.json()

json数据提取工具

pip install jsonpath

例子:通过jsonpath提取token值

data = {
    "content": {
        "data": {
            "titleMsg": "登录名或登录密码不正确",
            "resultCode": 100
        },
        "status": 0,
        "success": 'true'
    },
    "hasError": 'false'
}
resultCode = jsonpath(data, "$..resultCode")
print(resultCode)

注:返回的是列表,可以通过resultCode[0]提取

jsonpath需要两个参数:

注:

jsonpath表达式语法:

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

JsonPath (点击链接测试)结果
$.store.book[*].author获取json中store下book下的所有author值
$…author获取所有json中所有author的值
$.store.*所有的东西,书籍和自行车
$.store…price获取json中store下所有price的值
$…book[2]获取json中book数组的第3个值
$…book[-2]倒数的第二本书
$…book[0,1]前两本书
$…book[:2]从索引0(包括)到索引2(排除)的所有图书
$…book[1:2]从索引1(包括)到索引2(排除)的所有图书
$…book[-2:]获取json中book数组的最后两个值
$…book[2:]获取json中book数组的第3个到最后一个的区间值
$…book[?(@.isbn)]获取json中book数组中包含isbn的所有值
$.store.book[?(@.price < 10)]获取json中book数组中price<10的所有值
$…book[?(@.price <= $[‘expensive’])]获取json中book数组中price<=expensive的所有值
$…book[?(@.price <= $[‘expensive’])]获取json中book数组中price<=expensive的所有值
$…book[?(@.author =~ /.*REES/i)]获取json中book数组中的作者以REES结尾的所有值(REES不区分大小写)
$…*逐层列出json中的所有值,层级由外到内
$…book.length()获取json中book数组的长度

通过索引选择数组(列表中的数据)

jsonpath(data,'.data[1]′)jsonpath(data,′.data[1]') jsonpath(data,'.data[1]′)jsonpath(data,′.data[1].name')
jsonpath(data,'$.data[0][uid,name]')
jsonpath(data,'.data[?(@.uid==4)]′)jsonpath(data,′.data[?(@.uid==4)]') jsonpath(data,'.data[?(@.uid==4)]′)jsonpath(data,′.data[?(@.name==‘后台')]')

定位到data之后uid的数据

jsonpath(data,'$…name')

获取所有name,需使用上面过滤

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文