- 点击项目设置新建公共脚本
2. 添加脚本代码
// 获取环境变量-前置URL
const envPath = pm.environment.get('BASE_URL')
console.log(envPath);
const http = {
url: envPath + "/login",
method: "POST",
header: "Content-Type:application/json",
body: {
mode: "raw",
raw: JSON.stringify({ username: "admin", password: "123456"}),
// 序列化后的 json 字符串
}
}
// 远程API测试才需要请求token
pm.sendRequest(http, function (err, response) {
const res = response.json();
console.log(res.token);
//console.log(res.data.token);
pm.globals.set('token', res.token)
});
3.点击接口管理中接口目录设置token变量
4.调用接口测试
此时可以看到已经返回token
参考 https://blog.csdn.net/qq_53021672/article/details/125643067
https://www.yii666.com/blog/10386.html