上个接口的返回值,需要提取token传递到下个接口的请求头里面:

如图登录成功,产生的token:

json格式的请求参数放到body里面:

正则表达式提取:

提取器参数解释:

添加调式取样器查看token有没有被正则表达式提取到:

我这个接口需要在token前面拼接一个Bearer 的参数:

如图:

借组函数助手拼接:

参考这篇博客:https://www.cnblogs.com/yindada/p/11578308.html

请求结果:

传递token请求头信息:

用requests实现比jmeter简单多了

代码如下:

import requests
import os
BASE_PATH = os.path.dirname(os.path.abspath(__file__))  # todo 获取工程绝对路径
# print(BASE_PATH)
host = 'http://182.92.81.159'


def login():
    login_url = host + '/api/sys/login'
    headers = {"Content-Type": "application/json"}
    json = {
        "mobile": "13800000002",
        "password": "123456"
    }
    response = requests.post(url=login_url, json=json, headers=headers).json()
    return "Bearer" + ' ' + response.get('data')


def department():
    department_url = host+'/api/company/department/'
    headers = {"Authorization": login()}
    print(login())
    r = requests.get(url=department_url, headers=headers)
    print(r.text)


if __name__ == '__main__':
    login()
    department()

运行结果: