当前位置:网站首页>Interface test advanced interface script use - apipost (pre / post execution script)

Interface test advanced interface script use - apipost (pre / post execution script)

2022-07-08 00:35:00 InfoQ

Action time of pre execution script
A pre executed script is a script executed before a request is sent .
null
The role of pre executed scripts
Pre executed scripts can do the following :
  • To write JS Functions to achieve complex calculations ;
  • Variable printing
  • Definition 、 obtain 、 Delete 、 Clear environment variables
  • Definition 、 obtain 、 Delete 、 Clear global variables
  • Get request parameters
  • Dynamic addition 、 Delete one header Request parameters
  • Dynamic addition 、 Delete one query Request parameters
  • Dynamic addition 、 Delete one body Request parameters
  • send out HTTP request
To write JS Functions to achieve complex calculations
null
We can define a function in the pre execution script _random,
function _random(){
 return ' Hello , China ' + Math.random();
}
It returns a string :“ Hello China ”+ random number , At this time, you can use the
apt.globals.set("random_var", _random());
Assign it to a global variable random_var.
Pre execution script prints debug variables
We can go through console.log() Print the required variables on the console , To see the current value of a variable . As shown in the example above
null
Definition 、 obtain 、 Delete 、 Clear environment variables
apt.variables.set("key", "value"); //  Set a value to value Environment variables of key
apt.variables.get("key"); //  Get environment variables key Value
apt.variables.delete("key"); //  Delete environment variables key
apt.variables.clear(); //  Clear all defined environment variables
Definition 、 obtain 、 Delete 、 Clear global variables
apt.globals.set("key", "value"); //  Set a value to value Global variable of key
apt.globals.get("key"); //  Set a value to value Global variable of key
apt.globals.delete("key"); //  Set a value to value Global variable of key
apt.globals.clear(); //  Clear all defined global variables
Get request parameters
adopt request Object get request parameters , Details can be referred to 《APIPOST Built-in variables 》 Section 1 document .
Dynamic addition 、 Delete one header Request parameters
apt.setRequestHeader("key", "value"); //  Dynamically add a key to key The value is value Of header Parameters
apt.removeRequestHeader("key"); //  Delete header The key in the parameter is key Parameters of
Dynamic addition 、 Delete one query Request parameters
apt.setRequestQuery("key", "value"); //  Dynamically add a key to key The value is value Of query Parameters
apt.removeRequestQuery("key"); //  Delete query The key in the parameter is key Parameters of
Dynamic addition 、 Delete one body Request parameters
apt.setRequestBody("key", "value");//  Dynamically add a key to key The value is value Of body Parameters   Only aim at  form-data、urlencode It works
apt.removeRequestBody("key");// Delete body The middle key is key Parameters of   Only aim at  form-data、urlencode It works
send out HTTP request
We can use... In pre - executed scripts AJAX Of $.ajax Method to send a http request . Here is a simple example demo: Send a request to... Using a pre executed script
https://echo.apipost.cn/get.php
, And will respond to the results bigint Assigned to a global variable bigint.
$.ajax({
 url:"https://echo.apipost.cn/get.php",
 method:"POST",
 headers:{
 "content-type":"application/json"
 },
 timeout:"10000",
 async:false, //  Remember that this is set to  false, The request must be synchronized
 data:JSON.stringify({"email":"[email protected]","password":"123456"}), 
 success:function (response) {
 apt.globals.set("bigint",response.bigint); 
 }
}
Apipost Download address :
Apipost- Based on collaboration , More than API file 、 debugging 、Mock​
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072229499033.html