Postman (b): explains how to execute the request in the Pre-request Script


I. Pre-request Script here to introduce labels

Providing postman postman provides a "Send a request" code segments, he already method "Send a request" snippet packaged transmission request, he already sent the request encapsulated method.

Note: This code segment default can only send get request , and such words can not be used directly to specify the request class information, of course, in most cases we do not need to specify some of the requested information alone, postman can automatically help us to complete the preparatory work.


Second, write your own scripts to send post request

// 定义请求数据体
var data = {
    "userName":"admin",
    "password":"vfts123"
}

// 从环境变量中获取token请求服务地址
var ip = pm.environment.get("ip");
var port = pm.environment.get("port"); 
const loginRequest = {
  url: 'http://'+ ip+':'+ port +'/login',
  method: 'POST',
  header: ['Content-Type:application/json', 'token:123456'],
  body: {
    mode: 'raw',
    raw: JSON.stringify(data)
  }
};

// 发送请求
pm.sendRequest(loginRequest, function (err, res) {
    console.log(res.json());
    // 动态设置请求头token
    var result = eval(res.json());
    console.log('token is :' + result.data.token);
    pm.globals.set("login_token", result.data.token);
});

Guess you like

Origin www.cnblogs.com/kancy/p/11646464.html