ApiPost set global token

insert image description here

In order to avoid requesting the interface to request login every time and obtain token authentication, we can set a global token (token) to avoid using tokens alone everywhere and causing confusion in the environment. The usage is as follows:

interface settings

insert image description here
We first configure the request interface and request parameters, then initiate a request and obtain the request return body.

Set up pre-execution scripts

insert image description here

console.log("执行全局脚本");
// 延迟执行的函数
function delayedExecution() {
    
    
    $.ajax({
    
    
        "url": apt.variables.getPreUrl() + "/api/login",
        "method": "POST",
        "async": false,
        headers: {
    
    
            "content-type": "application/json"
        },
        "data": JSON.stringify({
    
     "username": "WH1201", "password": "888888" }),
        "success": function (response) {
    
    
            response = typeof response == "object" ? response : JSON.parse(response);
            console.log("执行成功:");
            console.log(response);
        }
    });
}

Execute the script after setting

insert image description here


// 清除之前设置的 token
apt.globals.set("token", null);
// 添加token
apt.globals.set("token", response.json.token);

In this way, we can see if our global environment has a new token
insert image description here

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/132304747