A collection of common methods for executing scripts before (after)

This article focuses on the set of common response parameter variables and common methods in the pre-execution and post-execution scripts of the interface management tool APIPOST.

Request: request parameter object

We can obtain the request parameter data from the request object in the pre-execution script or post-execution script. The object structure is as follows:

{" url ":" https://console.apipost.cn/need_token.php ", / / a string, the request url "method" : "POST", / / string, request way "timeout" : "Application /x-www-form-urlencoded", // string, request contentType" request_bodys": application/x-www-form-urlencoded", // string, request contentType" request_bodys": {// object, predefined request Body parameter "user_id": "1", "nick_name": "Jim green"}, "request_headers": {// Object, predefined request Header parameter "Authorization": "Basic amltOnB3ZDEyMw=="}}Copy the code

Response. raw: raw response data

What it means: Raw response data for the current request.

Response. Raw. The status / / response status code (200, 301, 404, etc.) the response. Raw. The responseTime / / response time (milliseconds) response. Raw. Type / / response types (json, etc.) The response. Raw. The responseText / / response textCopy the code

Response. json: Response data in JSON format

Description: The current request response data is in JSON format

Call example as above:

Response.json.data. token // Response.json.data ["token"]Copy the code

Response. headers: indicates the response head

Description: The response header of the current request

Call example:

Response.headers. Server // Response.headers ["server"]Copy the code

Response. cookies: Indicates a response cookie

Description: The response COOKIE of the current request applies to: after the script is executed

Call example:

The response. Cookies. PHPSESSION / / also can response. Cookies [" PHPSESSION "]Copy the code

ApiPost Collection of common methods

1. Set environment variables

apt.variables.set("key", "value"); // Set an environment variable key to value apt.variables.get("key"); // Get the value of the environment variable apt.variables.delete("key"); // Delete the environment variable key apt.variables.clear(); // Empty the environment variablesCopy the code

2. Set global variables

apt.globals.set("key", "value"); // Set a global variable key to value apt.globals.get("key"); // Get the value of the global variable key apt.globals.delete("key"); // Delete the global variable key apt.globals.clear(); // Clear global variablesCopy the code

Check if the Response Body contains a string

apt.assert('response.raw.responseText=="test"'); / / check whether the response text is equal to the test string apt. Assert (' response. Raw. The responseText. IndexOf (" test ") > - 1 '); // Check whether the response text contains the test stringCopy the code

Check whether a value in the returned JSON is equal to the expected value

apt.assert('response.json.hasOwnProperty("errcode")'); // Check whether the returned JSON object contains the errcode field apt.assert(' Response.json. errcode=="success"'); / / testing errcode column is equal to the success of a json object string apt. Assert (' response. Json. Errcode. IndexOf (" success ") > - 1 '); // Check whether the errcode field of the returned JSON object contains the success string apt.assert('response.json.errcode! ="success"'); // Check if the errcode field of the returned JSON object is not equal to the success string apt.assert('response.json.errcode>=1'); // Check whether the errcode field of the returned JSON object is greater than 1 apt.assert(' Response.json. errcode==null'); // Check whether the errcode field of the returned JSON object is nullCopy the code

Test whether an element of response Headers exists (e.g. Content-type)

apt.assert('response.headers.hasOwnProperty("content-type")');
Copy the code

6. Verify that the value of the Status code is 200

apt.assert('response.raw.status==200');
Copy the code

7. Verify that Response time is greater than a certain value

apt.assert('response.raw.responseTime>=100');
Copy the code