Pre-operations and post-operations are scripts that are executed during the sending and response process of API requests, and are mainly used to complete validation or perform certain operations before initiating an API request and after obtaining a response, in order to improve the efficiency of API debugging and testing and ensure the correctness of the API.
Prepants are script code that executes before an API request and can be used to do the following:
- Set API Headers (request headers)
They can be used to set request headers, request bodies, validate request parameters, configure authentication, and more.
- Set API Parameters
Predecessors have access to data in environment, global, and request variables. Predecessors can also help requestors understand request parameters and how to handle them.
- Add Authentication
For example, Basic authentication or OAuth. Before sending an API request, precursors can be used to obtain an access token or other permissions to ensure that the API request sends a valid and legitimate request.
The "variable substitution" function is usually used to replace all referenced variables (including dynamic values) in the API request parameters with the actual request content before sending an API request. It is typically used to handle conversion scenarios such as API signatures. At this time, the following two scenarios are mainly involved:
- Set Variables via Script
The operation in this scenario needs to be run before "variable substitution", otherwise the variable set through the script will not take effect on the request parameters of the current API.
- API Signing Scripts
In this scenario, the script needs to be placed after "variable substitution" so that the script can obtain the parameter value actually requested by the API, otherwise the obtained parameter value is the template data before variable substitution.For more information, please refer to How to Handle API Signatures?
The post-operation can use the data obtained from the API response, such as status code, header, body and other information, for secondary processing:
- Verify that the status code and response time of the API response are as expected.
- Validate the content of the API response, such as JSON or XML data.
- Extract data from API responses and use it for subsequent requests.
- Automatically extract the data needed in the response.
Both pre-action and post-action can set parent actions in the API catalog. The parent operation can be inherited to all APIs in the directory, which is suitable for scenarios where the same precursor operation needs to be performed in multiple APIs, such as authentication, variable substitution, etc. The API itself can flexibly adjust the order in which each operation is run.