Postman

设置 Header 中的 token

1
2
3
4
5
6
7
8
9
10
11
12
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

var r = JSON.parse(responseBody);

var token = r.data.token;
console.log(token);

pm.globals.unset("LoginToken");

pm.globals.set("LoginToken", "Bearer:" + token);

如何使用 Postman 对接口参数进行签名

Pre-request Script 选项卡中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
// 获取当前的请求路径
var url = pm.request.url;

// 获取环境变量
const client = pm.environment.get("client");
const reqVersion = pm.environment.get("reqVersion");
const sign = pm.environment.get("reqVersion");

// 将必填参数拼接到路径上
url = url + "&client=" + client + "&reqVersion=" + reqVersion + "&sign=" + sign;

// 重写url
pm.request.url = url;

SHA256 签名示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 获取当前的请求路径
var url = pm.request.url;

// 配置参数
let activeId = "2197";
let awardId = 10266;
let cOpenId = "aaaa";
let MAC = "001a9a000000";
let cUDID = "23320005";
let accessToken = "bbb";
let cChip = "ccc";
let cEmmcCID = "ddd";
let cModel = "eee";

// 签名
let signStr = `MAC=${MAC}&accessToken=${accessToken}&cChip=${cChip}&cEmmcCID=${cEmmcCID}&cModel=${cModel}&cOpenId=${cOpenId}&cUDID=${cUDID}&id=${activeId}&source=wechat`;
var sign = CryptoJS.SHA256(signStr).toString();

// 将必填参数拼接到路径上
url = `${url}?activeId=${activeId}&awardId=${awardId}&cOpenId=${cOpenId}&MAC=${MAC}&cUDID=${cUDID}&accessToken=${accessToken}&&token=${sign}`;

// 重写url
pm.request.url = url;