Solve the problem of invalid token variable setting in Apipost in ajax request

Table of contents

Summary and solutions


Today, token verification was added to the backend interface. When using the apipost directory to add public headers and pre-execution scripts, the results were inconsistent with expectations. Refer to apipost official documentation

Add public headers to all directories that need to be verified with tokens as follows:

 Add the following pre-execution script to all directories that need to be verified with tokens:

$.ajax({
    url: "http://localhost:8080/user/login",
    method: "GET",
    headers: {
        "content-type": "application/json"
    },
    timeout: "10000",
    data: JSON.stringify({
        "account": "my_account",
        "password": "my_password"
    }),
    success: function (response) {
        console.log(response.data.token);//打印查看此次登陆获取的token,稍后与变量参数的值比对
        apt.variables.set("token", response.data.token);//设置环境变量token为此次返回的token
    }
});

According to the expected results, as long as I configure these two places, when using apipost to execute an interface that requires token verification,

The pre-execution script will be used to perform a login operation first, obtain the token for this login, and store it in the environment variable. Then the token value at the header will take the value of this environment variable token.

That is, it is expected that a token will be carried to the backend normally in the header, and this token will be updated every time it is executed.

However, when these two executions are configured, the token value is still { {token}}, and we look at the small eye on the right to check the current environment variables, and there is no token, indicating that we did not successfully set the environment variables in the ajax request.

The description of the official document where the token value is still { {token}} is as follows, saying that the variable token has not yet been bound to the token returned by the interface, so it is output as is.

 But I clearly set the returned token into a variable after the ajax request?

As for the reason why setting environment variables in ajax is invalid, I have not seen a description in the official documents, and the official documents also have such setting of environment variables in ajax.

So instead of using the pre-execution script to set the token variable, I set the token to the variable in the post-execution script of the login interface, as follows.

apt.globals.set("token", response.json.data.token);

At this time, we first execute the login interface, get the token for this login, and then execute the interface that requires token verification. The execution is successful, which means that setting the variables is no problem, but setting the environment variables in ajax does not take effect.

But why do we set the environment variables invalidly in the ajax request?

After looking at the problem I encountered one night, I suddenly remembered

ajax is asynchronous.

Taking a closer look at the official documentation, I found that it is stated in the description that when executing the ajax method, the request may have been sent during the execution process, which means that the variables we set in ajax did not catch up with the speed of its sending.

Therefore, apipost can be converted to a synchronous method through the await keyword after version 7.0.4. The function is executed first and then the interface is sent.

Summary and solutions

Ajax is an asynchronous request. Before the function executes and we reach the line where the token environment variable is set, the request has already been sent, so our token value is not obtained.

Then add the await keyword before the ajax request to convert the ajax into a synchronous request and ensure that our environment variables are set before sending the request, as follows.

await $.ajax({
    url: "http://localhost:8080/user/login",
    method: "GET",
    headers: {
        "content-type": "application/json"
    },
    timeout: "10000",
    data: JSON.stringify({
        "account": "my_account",
        "password": "my_password"
    }),
    success: function (response) {
        console.log(response.data.token);
        apt.variables.set("token", response.data.token);
    }
});

Guess you like

Origin blog.csdn.net/m0_54250110/article/details/129459436