[Recommended] Interface testing tool-Definition and use of Apipost variables

What is a variable?
For some common parameters, we can define them as variables, in order to achieve the purpose of one change, all changes.

For example, the URL domain name we requested can be set as a variable, and just enter the route in the URL column. In this case, we can achieve the API interface that requests different domain names and the same route by controlling the value of the variable (for example, it can be used to quickly switch between the development environment and the production environment).

As shown in the figure below, we have defined the domain name as a variable: url.
Insert picture description here

How to define variables?
ApiPost V5 supports 2 ways to define variables: predefined and scripted. Let's talk about it separately.

1. ApiPost predefined variables
Click the environment manager in the upper right corner, click [New Environment], and you can define variables in the pop-up window.
Insert picture description here

As shown in the figure, we define the variable: url here, and assign it to: 127.0.0.1:8888/login.
At this time, when we click Send, { {url}} will be automatically replaced with 127.0.0.1:8888/ login.
Of course, in addition to the URL, the request parameters can also be replaced by variables. As shown below:
Insert picture description here

I newly defined a variable { {name}} and put it in the body request parameter. After sending it, the server successfully received the variable I defined.

Insert picture description here
Insert picture description here

2. Variables defined by ApiPost script The
new version of ApiPost V5 supports variable definitions through JS scripts. In this case, our variable richness will be much more.
Just like the previous example, I set a variable url through the script, and the effect is the same as the previous one. As shown below:
Insert picture description here

Seeing this, you may notice: here you can set global variables and environment variables. So what are global variables? What are environment variables? What is the difference between them?
Environment variables:
Environment variables only take effect in the currently selected environment. When you switch the current environment in the upper right corner, the environment variables become invalid.
Common usage scenarios such as the above examples, we can define the development environment and production environment, so as to specify different URLs to distinguish the different interface request addresses.
Insert picture description here

Global variables:
Global variables are not affected by the current environment, as long as they are in the same project, the variables will take effect.
The method of calling environment variables and global variables is the same { {variable name}}.

3. ApiPost script defines complex variables.
ApiPost supports custom functions to define complex variables. As shown below:

Insert picture description here
Insert picture description here

In the above example, we set a function _random,
function _random(){ return'Hello, China' + Math.random();}
It returns a string: "Hello China" + random number, and passed
apt.globals.set("random_var", _random());
has been assigned. After sending, the server successfully received it.

Guess you like

Origin blog.csdn.net/weixin_47719617/article/details/113989157