[share] ApiPost common methods collection of pre (post) execution script

This paper mainly introduces the common response parameter variables and common method sets in the pre execution script and post execution script of API post, an interface management tool.

About ApiPost:

ApiPost is an API debugging and management tool that supports team collaboration and can generate documents directly. It supports the simulation of POST, GET, PUT and other common requests, and is a rare tool for back-end interface developers or front-end and interface testers.

ApiPost response parameter variable

response.raw : raw response data

Call example:

response.raw.status //Response status code (200, 301, 404, etc.)
response.raw.responseTime //Response time (MS)
response.raw.type //Response type (json, etc.)
response.raw.responseText //Response text

response.json : response data in JSON format (this is what the above example uses)

The call example is as follows:

response.json.data.token //it's fine too response.json.data["token"]

response.headers : response header

Call example:

response.headers.server //it's fine too response.headers["server"]

response.cookies : response cookie

Call example:

response.cookies.PHPSESSION //it's fine too response.cookies["PHPSESSION"]

ApiPost common methods collection:

1. Setting environment variables

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

 

2. Set global variables

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

 

3. Check whether the response body contains a string

apt.assert('response.raw.responseText=="test"');  // Check that the response text is equal to the test string
apt.assert('response.raw.responseText.indexOf("test") > -1');  // Check whether the response text contains the test string

 

4. Checks 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"');  // Check whether the errcode field of the returned json object is equal to the success 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 whether 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 null

 

5. Test whether an element in response Headers exists (such as content type)

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

 

6. Verify that the Status code value is equal to 200

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

 

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

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

 

8. Get request parameters:

We can obtain the requested parameter data through the request object in the pre execution script. The object structure is as follows:

{
    "url": "https://console.apipost.cn/need_token.php"// String, request URL
    "method": "POST",    // String, request mode
    "timeout": 25000,    // Number, request timeout limit (MS)
    "contentType": "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 parameters
        "Authorization": "Basic amltOnB3ZDEyMw=="
    }
}

Tags: Programming JSON PHP

Posted on Wed, 03 Jun 2020 07:53:07 -0700 by Kevin3374