json文件的请求是我们学习测试以及编写代码必须要知道的,例如在POST请求中另一个经常用到的参数是json,如果请求头中Content-Type对应的value值是application/json,入参一般是data类型,那么我们就可以使用json序列化入参方式进行尝试仔Python中编写该请求的代码,如果是json格式那就不需要做序列化
场景如下:现有某CSDN的登录页面接口请求信息以及一个以Json文件类型梳写好的登录请求参数信息,我们需要在里面去取对应的参数作为入参
01.测试请求信息
CSDN请求登陆页面接口请求相关信息
代码语言:javascript复制
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://blog.csdn.net/wshlp123456/article/details/89194095; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1591328229,1591339908,1591426433,1591426556; announcement=%7B%22isLogin%22%3Atrue%2C%22announcementUrl%22%3A%22https%3A%2F%2Fbss.csdn.net%2Fm%2Ftopic%2Flive_recruit%3Futm_source%3Dannounce0515%22%2C%22announcementCount%22%3A0%2C%22announcementExpire%22%3A3600000%7D; c_ref=https://blog.csdn.net/tester_sc; SESSION=cf7f309a-7d85-482e-ba96-046ea2f23861; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac={"islogin":{"value":"1","scope":1},"isonline":{"value":"0","scope":1},"isvip":{"value":"0","scope":1},"uid_":{"value":"tester_sc","scope":1}}; 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 SvHojjT9aXr3SYo8Jk8lJeHQl86AOCz8t9IGEvOAigIbzEc1F2rVUUWlX2TrFuX/djK5cdJETN 1OlliiMNCz3qrBs9APnvcgQcDg23NaVrTbKV8R23TDDJu NQXm5/Ba6Vvom5Z8D HDWwf6EId0VlLvSSwwErNlC5sZ2FTUZ8kRXoevFXPpQkqrJ2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UNUsIta3jtZsaoTmROJHf4OH2xTGwQ/D/tDKEmaf1nG63Jz2g1F5EkBJVcr4MU/F5eVdjGS26vraJs3XHFGNT8HSbVG85="
userIdentification: "695214899@qq.com"
webUmidToken: "T68B9D4FE5D5F6DD0A24CACC882FC89BFC2B67F9DF2F9B6488E0B8109F4"
02.json入参文件信息
我们自行创建的 login.json 文件,Json文件内参数内容
1.一个登录请求入参
2.一个添加购物车请求的入参
代码语言:javascript复制{
"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/Ba6Vvom5Z8D HDWwf6EId0VlLvSSwwErNlC5sZ2FTUZ8kRXoevFXPpQkqrJ2e7yqpwQVu1NnP/J11BGX6xoHHvHyMinJtoSUQ0buDQma4c1ooC4UmZJfmhb5X2pLZ7idm/f vpi2sBWHKIzARiXYXCB4CjiLq9Yw8kCO ZS 9JYf2c5TK8OqSk1Lcp/rtghnmajPevwqingpCjPAmD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E0B8109F4"
},
"addcard": {
"name": "apple",
"amount": "15",
"price": "0.5"
}
03.Python实操
那么我们如何在python中将以上的接口请求运行起来呢,我们一起来看看,创建一个 operationjson.py 用于此次代码的编写,我们可以将 login.json跟它放在同一级目录,进入代码
代码语言:javascript复制# 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:python3python_codeStudylogin.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参数实战分享就到这里,不断学习,不断进步。
- THE END-