tonken讲解__post_python_jmeter
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()
第二种方法:
import re
import requests
class tonken():
def __init__(self):
pass
def uid(self):
url="http://manage.duoceshi.com/auth/code"
h={"Content-Type":"application/json"}
r=requests.get(url=url,headers=h)
# print(r.text) #返回参数
wb=r.text
s=re.findall('"uuid":"(.+?)"',wb)
print(s)
dict1 = {}
dict1['uuid'] = s[0]
for i,j in dict1.items():
print(j)
return j
def login(self):
c=self.uid()
url1="http://manage.duoceshi.com/auth/login"
h1 = {"Content-Type": "application/json"}
data1={"username":"admin",
"password":"AL2HPFkHICFLSF7Nh0HBWqKf2XbZh9aBqM01ygQmQl61eLZVjxjXVp3/KtTwUzGiTHIhBXmdEExVzD4RoIYdhA==",
"code":"8888",
"uuid":c#引用上一个接口
}
r1=requests.post(url=url1,json=data1,headers=h1) #备注data数据要json格式,才能查看到返回数据
print(r1.text)
wb1=r1.text
s = re.findall('"token":"(.+?)"', wb1)
print(s)
dict2 = {}
dict2['uuid'] = s[0]
for i, j in dict2.items():
print(j)
return j
def build(self):
url2="http://manage.duoceshi.com/api/menus/build"
h2={"Authorization":self.login()}
r2=requests.get(url=url2,headers=h2)
print(r2.json())
if __name__ == '__main__':
dx=tonken()
# dx.uid()
# dx.login()
dx.build()
第三种方法
import re
import requests
class tonken():
def __init__(self):
pass
def uid(self):
url="http://manage.duoceshi.com/auth/code"
h={"Content-Type":"application/json"}
r=requests.get(url=url,headers=h)
# print(r.text) #返回参数
wb=r.text
s=re.findall('"uuid":"(.+?)"',wb)
print(s)
m=" ".join(s)
print(m) #code-keyd6f4494345ba49679d80592465f13305
return m
def login(self):
c=self.uid()
url1="http://manage.duoceshi.com/auth/login"
h1 = {"Content-Type": "application/json"}
data1={"username":"admin",
"password":"AL2HPFkHICFLSF7Nh0HBWqKf2XbZh9aBqM01ygQmQl61eLZVjxjXVp3/KtTwUzGiTHIhBXmdEExVzD4RoIYdhA==",
"code":"8888",
"uuid":self.uid()#引用上一个接口
}
r1=requests.post(url=url1,json=data1,headers=h1) #备注data数据要json格式,才能查看到返回数据
print(r1.text)
wb1=r1.text
s = re.findall('"token":"(.+?)"', wb1)
print(s)
n=''.join(s)
print(n)
def build(self):
url2="http://manage.duoceshi.com/api/menus/build"
h2={"Authorization":self.login()}
r2=requests.get(url=url2,headers=h2)
print(r2.json())
if __name__ == '__main__':
dx=tonken()
# dx.uid()
# dx.login()
dx.build()
==========================================================
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
@--
再设置uuid变量
第二个接口:
POST http://manage.duoceshi.com/auth/login
POST data:
设置提取方法
请求头中设置了变量
第三个接口: