postman在接口请求之前先请求其他接口

Pre-request Script中获取另一个请求的返回数据

Pre-request Script中发送get请求

const reqshop = {
    url : "https://sewew/shops/" + pm.environment.get("sid") +"/neuron",
    method: 'GET',
    header: 'Authorization:Bearer '+pm.environment.get("flashcardToken")
}
pm.sendRequest(reqshop, function (err, response) {
    var resurl = response.json().url;
    var willremove="https://232m/#/?auth_token=";
    var token = resurl.replace(willremove,"");
    pm.environment.set("token", token);
});

Pre-request script中发送post请求

const req = {
    url: 'https://wewe/account',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(
        {
            "redirect": "/sites/",
            "username": "username",
            "password": "password",
            "autoLogin": true
        }
        )
    }
};
pm.sendRequest(req, function (err, response) {
    var flashcardToken = response.json().data.user.token
    pm.environment.set("flashcardToken", flashcardToken);
});

你可能感兴趣的:(Postman,postman,测试工具)