Postman请求前获取Token

代码如下:

 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

var token

const loginRequest = {
    url: login_url,
    method: 'POST',
    header: 'Content-Type: application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify({"account":"junjie001@qq.com","pass":"Hello123","level":1,"inviteId":"","type":0})
    }
}

pm.sendRequest(loginRequest,function(err,res){
    
    if(err) {
        console.log(err)
    } else {
        
        var response = JSON.parse(res.text());

        token = response.data.token

        postman.setEnvironmentVariable('token', token)

        console.log(response.data.token);

    }
});


参考教程

  1. Postman脚本中发送请求
  2. js字符串转换为对象格式
  3. Postman:脚本应用_预请求脚本