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

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

2022-08-10 18:19:00 InfoQ

The action time of the pre-execution script
The pre-execution script is a script that is executed before the request is sent.
null
The role of pre-execution scripts
Pre-execution scripts can accomplish the following functions:
  • Write JS functions to realize complex calculations;
  • Printing of variables
  • Define, get, delete, clear environment variables
  • Define, get, delete, clear global variables
  • Get request parameters
  • dynamically add or delete a header request parameter
  • dynamically add or delete a query request parameter
  • Dynamicly add or delete a body request parameter
  • Send HTTPRequest
Write JS functions to implement complex calculations
null
We can define a function _random in the pre-executed script,
function _random(){
return 'Hello, China' + Math.random();
}
It returns a string: "Hello China" + random number, which can be passed
apt.globals.set("random_var", _random());
Assign it to the global variable random_var.
Pre-execution script prints debugging variables
We can print the required variables to the console through console.log() to view the current value of a variable.As shown in the example above
null
Define, get, delete, clear environment variables
apt.variables.set("key", "value"); //Set an environment variable key
apt.variables.get("key"); // Get the value of the environment variable key
apt.variables.delete("key"); //Delete the environment variable key
apt.variables.clear(); // Clear all defined environment variables
Define, get, delete, clear global variables
apt.globals.set("key", "value"); //SetA global variable key
apt.globals.get("key"); //Set a global variable key
apt.globals.delete("key"); //Set a global variable key with value value
apt.globals.clear(); //Clear all defined global variables
Get request parameters
PassThe request object obtains request parameters. For details, please refer to the document "APIPOST Built-in Variables".
Dynamicly add or delete a header request parameter
apt.setRequestHeader("key", "value"); // Dynamically add a header whose key is key and value is valueParameters
apt.removeRequestHeader("key"); //Remove the parameter whose key is key in the header parameter
dynamically add or delete a query request parameter
apt.setRequestQuery("key", "value"); // Dynamically add a query parameter whose key is key and value is value
apt.removeRequestQuery("key"); //Remove the key in the query parameter as keyThe parameters
dynamically add and delete a body request parameter
apt.setRequestBody("key", "value");// Dynamically add a key as the key valueThe body parameter is only valid for form-data and urlencode
apt.removeRequestBody("key");//Remove the parameter whose key is key in the body Only valid for form-data and urlencode
Sending HTTP Requests
We can use AJAX's $.ajax method to send an http request in a pre-executed script.The following is a simple example demo: use a pre-executed script to send a request to
https://echo.apipost.cn/get.php
, and assign the bigint of the response result to the global variable bigint.
$.ajax({
url:"https://echo.apipost.cn/get.php",
method:"POST",
headers:{
"content-type":"application/json"
},
timeout:"10000",
async:false, // remember thisIf the item 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, not limited to API documentation, debugging, Mock​




———————————————
Copyright statement: This article is an original article by CSDN blogger "Elegance...", following the CC 4.0 BY-SA copyright agreement, please attach the original source for reprintingLink and this statement.
Original link:
Interface test advanced interface script use-apipost (pre/post-execution script)_Elegance... Blog-CSDNBlog
原网站

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