token案例:
案例:
网站:http://shop.duoceshi.com/login?redirect=%2Fdashboard
code接口:http://manage.duoceshi.com/auth/code
登录接口:http://manage.duoceshi.com/auth/login
登录接口参数:{"username":"admin","password":"e6WGOz+g/FuR646O7IF8JrlC6qH/anCI9/0UCsVDnUxN2aBdGKtRffNb1W7i87dRavZCNyP9yqvAcXLgdKtsRA==","code":"8888","uuid":"{{uuid}}"}
链接类型:Content-Type:application/json
搜索商品接口:http://manage.duoceshi.com/api/yxStoreCategory?page=0&size=10&sort=sort%2Cdesc
Authorization:
Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY4MDE0OTgwNX0.fmlEdR5Xh1BF2al6crfPMjG8dd-M6TOKux3wni5ejvGFLce4Zk0tvKsCobj7WEaXvXI-N-21AIj2HH5LFlFMNw
一、postman工具实现
第一个接口:
code接口:http://manage.duoceshi.com/auth/code
test中的语句:
//需要拿到uuid接口返回值里面的uuid作为下一个接口请求头里面的入参
// 1.通过JSON.parse函数把responseBody响应体进行格式化为json字符串
var jsonData = JSON.parse(responseBody);
// 2.通过json字符串里面的uuid这个键拿到对应的uuid值
postman.setEnvironmentVariable("uuid",jsonData.uuid);
第二个接口:
登录接口:http://manage.duoceshi.com/auth/login
body参数:
{"username":"admin","password":"e6WGOz+g/FuR646O7IF8JrlC6qH/anCI9/0UCsVDnUxN2aBdGKtRffNb1W7i87dRavZCNyP9yqvAcXLgdKtsRA==","code":"8888","uuid":"{{uuid}}"}
test语句:
//需要拿到登录接口返回值里面的token作为下个接口请求头里面的入参
// 1.通过JSON.parse函数把response响应体进行格式化为json字符串
var jsonData = JSON.parse(responseBody);
// 2.通过json字符串里面的token这个键拿到对应的token值设为环境变量
postman.setEnvironmentVariable("token", jsonData.token);
第三个接口:
搜索接口:
http://manage.duoceshi.com/api/yxStoreCategory?page=0&size=10&sort=sort%2Cdesc
==========================================================
python 获取tonken
import requests def uid():url="http://manage.duoceshi.com/auth/code"h={"Content-Type":"application/json"}r=requests.get(url=url,headers=h)print(r.text) #返回参数u=r.json()["uuid"]print(u)return r.json()['uuid'] #在返回参数中提取 def login():url1="http://manage.duoceshi.com/auth/login"h1 = {"Content-Type": "application/json"}data1={"username":"admin","password":"AL2HPFkHICFLSF7Nh0HBWqKf2XbZh9aBqM01ygQmQl61eLZVjxjXVp3/KtTwUzGiTHIhBXmdEExVzD4RoIYdhA==","code":"8888","uuid":uid() #引用上一个接口}r1=requests.post(url=url1,json=data1,headers=h1) #备注data数据要json格式,才能查看到返回数据print(r1.text)print(r1.json())t=r1.json()["token"]print(t)return r1.json()['token'] #在返回体中提取token def bid():url2="http://manage.duoceshi.com/api/menus/build"h2={"Authorization":login()}r2=requests.get(url=url2,headers=h2)print(r2.json()) if __name__ == '__main__':# uid()# login()bid()
第二种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
第三种方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
========================================================== jmeter中tonken
第一个接口:GET http://manage.duoceshi.com/auth/code 第二个接口:POST http://manage.duoceshi.com/auth/login
{"username":"admin","password":"ogqJt/paTDJCulj4u06yrGhkwH7XwiNe0Ce6ToKxUaPRjASkd+RHIp8knCihuLpsL5I+5p7NCp7mClZ04U9vog==","code":"8888","uuid":"code-key1867ef6878954530a3b5ce2a3b8ead00"}
第三个接口:GET http://manage.duoceshi.com/api/menus/build第一种方法步骤:
第一个接口:GET http://manage.duoceshi.com/auth/code
提取:
第二个接口:POST http://manage.duoceshi.com/auth/login 用户参数:
{"username":"admin","password":"ogqJt/paTDJCulj4u06yrGhkwH7XwiNe0Ce6ToKxUaPRjASkd+RHIp8knCihuLpsL5I+5p7NCp7mClZ04U9vog==","code":"8888","uuid":"code-key1867ef6878954530a3b5ce2a3b8ead00"}
token值提取:
第三个接口:
GET http://manage.duoceshi.com/api/menus/build
第二种方法:
通过JSON Extractor 方法
第一个接口:GET http://manage.duoceshi.com/auth/code
在设置uiid变量
第二个接口:
POST http://manage.duoceshi.com/auth/login
POST data:
{"username":"admin","password":"ogqJt/paTDJCulj4u06yrGhkwH7XwiNe0Ce6ToKxUaPRjASkd+RHIp8knCihuLpsL5I+5p7NCp7mClZ04U9vog==","code":"8888","uuid":"code-keyc156c7a4911e464790e2998ce068240f"}
设置提取方法
请求头中设置了变量
第三个接口:
GET http://manage.duoceshi.com/api/menus/build