首页 » 技术分享 » Python操作Json文件之JSON参数实战

Python操作Json文件之JSON参数实战

 

json文件的请求是我们学习测试以及编写代码必须要知道的,例如在POST请求中另一个经常用到的参数是json,如果请求头中Content-Type对应的value值是application/json,入参一般是data类型,那么我们就可以使用json序列化入参方式进行尝试仔Python中编写该请求的代码,如果是json格式那就不需要做序列化

场景如下:现有某CSDN的登录页面接口请求信息以及一个以Json文件类型梳写好的登录请求参数信息,我们需要在里面去取对应的参数作为入参

01.测试请求信息

CSDN请求登陆页面接口请求相关信息



General
Request URL: https://passport.csdn.net/v1/register/pc/login/doLogin
Request Method: POST
Status Code: 400 
Remote Address: 101.201.169.146:443
Referrer Policy: unsafe-url


Request Headers
:authority: passport.csdn.net
:method: POST
:path: /v1/register/pc/login/doLogin
:scheme: https
accept: application/json, text/plain, */*
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
content-length: 1998
content-type: application/json;charset=UTF-8
cookie: uuid_tt_dd=10_36586516840-1587717225136-370121; dc_session_id=10_1587717225136.513733; __gads=ID=c2db412e8df15c9d:T=1587717226:S=ALNI_MbYL4RFW5ZKcfC9oe1Q9S4RdO8DzA; dc_sid=cddcb30af3a1cdf554c4923ac08b7354; c_first_ref=www.baidu.com; TY_SESSION_ID=a4bd90e5-4dff-48f0-9612-161f3043cc8b; UN=tester_sc; BT=1588988903903; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_36586516840-1587717225136-370121!5744*1*tester_sc; c_utm_source=blogxgwz3; aliyun_webUmidToken=T3DBFBD53223E68DDFEADD225D4D94C8666740FED8186ADC89B2E2C94A6; c_first_page=https%3A//blog.csdn.net/wshlp123456/article/details/89194095; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1591328229,1591339908,1591426433,1591426556; announcement=%257B%2522isLogin%2522%253Atrue%252C%2522announcementUrl%2522%253A%2522https%253A%252F%252Fbss.csdn.net%252Fm%252Ftopic%252Flive_recruit%253Futm_source%253Dannounce0515%2522%252C%2522announcementCount%2522%253A0%252C%2522announcementExpire%2522%253A3600000%257D; c_ref=https%3A//blog.csdn.net/tester_sc; SESSION=cf7f309a-7d85-482e-ba96-046ea2f23861; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22tester_sc%22%2C%22scope%22%3A1%7D%7D; dc_tos=qbhy3j; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1591433695
origin: https://passport.csdn.net
referer: https://passport.csdn.net/login
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
x-requested-with: XMLHttpRequest
x-tingyun-id: im-pGljNfnc;r=433788193




Request Payload
{loginType: "1", pwdOrVerifyCode: "123456", userIdentification: "695214899@qq.com",…}
loginType: "1"
pwdOrVerifyCode: "123456"
uaToken: "123#Z3DDbDyePRHnIDbxlDnP8ldEzQXHO1A9926XsWLyll/HzaHKDy0d6RKAfAIMr6I0mHoWoX8WU+Cxu5lVzE2Z1wU0hJhfKl8UMTxtD3qJwyz9foagjKFBgBSFvsfnUEROhKqrw/sqrlhBC2jaapUtmZgokP2jzoin2ZJrKiGjNVzYsnQXdN58eRwHGeclOIn2HRCRL+z6P0pH4S3ZMKPtNFzsskFdhzcyCAzYGTJP8lYlQGMK6g3iOZUrIZB0/RGUk7bK1ExO2dDLKoU4vJxJnyH+xuUhBw6nzddZ6C3thXrKtYG3nWOpl54P5D+rwKNAp5LQacd69vkOv+ZXDbMiXaMRbm+DjoFfEcYVrLg5/qDOhBqQbuIKRSFqWmkr/A1Ha/Z0t1s6CAwLfCe1+OwJ8+umjowpY5k/kBAFywonobCO1ET24dmd+SvHojjT9a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SlU07ai4hzsE1CtjARYBLbqa1/KHNMYdpo3qSg4n343OE6ugSoBwZeQm+SA9VYKvyEOEOfxbIiEGJBICkLxH8n0AwZkXxtrAIoTKUYGFpAW0C1Q/PMC+JrLiTPXaL9HH5VEORHW4QPLzh9A3QePBhwna9NRDnsU2FkmFkKFx/oLRsSZFA+q4xK1gcuZOeciiCZRNUNUsIta3jtZsaoTmROJHf4OH2xTGwQ/D/tDKEmaf1nG63Jz2g1F5EkBJVcr4MU/F5eVdjGS26vraJs3XHFGNT8HSbVG85="
userIdentification: "695214899@qq.com"
webUmidToken: "T68B9D4FE5D5F6DD0A24CACC882FC89BFC2B67F9DF2F9B6488E0B8109F4"

02.json入参文件信息

我们自行创建的 login.json 文件,Json文件内参数内容

1.一个登录请求入参

2.一个添加购物车请求的入参

{
  "login": {
    "usename": "95214899@qq.com",
    "loginType":"1",
    "passwd": "123456",
    "uaToken":"123#Z3DDbDyePRHnIDbxlDnP8ldEzQXHO1A9926XsWLyll/HzaHKDy0d6RKAfAIMr6I0mHoWoX8WU+Cxu5lVzE2Z1wU0hJhfKl8UMTxtD3qJwyz9foagjKFBgBSFvsfnUEROhKqrw/sqrlhBC2jaapUtmZgokP2jzoin2ZJrKiGjNVzYsnQXdN58eRwHGeclOIn2HRCRL+z6P0pH4S3ZMKPtNFzsskFdhzcyCAzYGTJP8lYlQGMK6g3iOZUrIZB0/RGUk7bK1ExO2dDLKoU4vJxJnyH+xuUhBw6nzddZ6C3thXrKtYG3nWOpl54P5D+rwKNAp5LQacd69vkOv+ZXDbMiXaMRbm+DjoFfEcYVrLg5/qDOhBqQbuIKRSFqWmkr/A1Ha/Z0t1s6CAwLfCe1+OwJ8+umjowpY5k/kBAFywonobCO1ET24dmd+SvHojjT9aXr3SYo8Jk8lJeHQl86AOCz8t9IGEvOAigIbzEc1F2rVUUWlX2TrFuX/djK5cdJETN+1OlliiMNCz3qrBs9APnvcgQcDg23NaVrTbKV8R23TDDJu+NQXm5/Ba6Vvom5Z8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H5VEORHW4QPLzh9A3QePBhwna9NRDnsU2FkmFkKFx/oLRsSZFA+q4xK1gcuZOeciiCZRNUNUsIta3jtZsaoTmROJHf4OH2xTGwQ/D/tDKEmaf1nG63Jz2g1F5EkBJVcr4MU/F5eVdjGS26vraJs3XHFGNT8HSbVG85=",
    "webUmidToken": "T68B9D4FE5D5F6DD0A24CACC882FC89BFC2B67F9DF2F9B6488E0B8109F4"
  },


  "addcard": {
    "name": "apple",
    "amount": "15",
    "price": "0.5"
  }

03.Python实操

那么我们如何在python中将以上的接口请求运行起来呢,我们一起来看看,创建一个 operationjson.py 用于此次代码的编写,我们可以将 login.json跟它放在同一级目录,进入代码

# coding=utf-8


import requests
import  
# 引用requests json 模块


def getHeaders():
    headers = {
        'content-type': 'application/json;charset=UTF-8',
        'user - agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
    }
    return headers
# 定义请求头类 添加 "content-type "" user - agent "


fp = open('F:\python3\python_code\Study\login.json')
datas = json.load(fp)
print(datas['login'])
# python进行操作json文件,打开我们的login.json'文件将参数写入变量下面引用


def logins():
    r = requests.post(
        url='https://passport.csdn.net/v1/register/pc/login/doLogin',
        data=json.dumps(datas['login']),
        headers=getHeaders()
    )
    print(json.dumps(r.json(), indent=True, ensure_ascii=False))
# python进行对应json格式参数接口测试实战


if __name__ == '__main__':
    logins()

此次只是基于python接口测试方面的学习,如果在现实生活中单单几个接口没必要这么大费周章,很多开源工具可直接跑单个业务或者多个业务的接口测试,自动化的目的就是复杂事情简单做,简单事情尽量不做。Python操作Json文件之JSON参数实战分享就到这里,不断学习,不断进步!

基于python接口自动化测试的资料小编整理出来推荐大家学习公众号后台回复0607领取

- THE END-

近期推出福利优惠测试技术进阶提升圈子以及知识星球点击原文链接或者戳#文字链接查看详情#知识星球# #测试提升圈#

如果您还想了解更多测试的相关知识,请关注下方二维码:测试资料集合更新了,可在公众号后台回复"989""python"进行领取最新资料包目录如下

"关注Python测试社区,查看更多精彩文章"

点击「在看」让更多人看到!

转载自原文链接, 如需删除请联系管理员。

原文链接:Python操作Json文件之JSON参数实战,转载请注明来源!

0