Skip to main content

Introduction

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 or after obtaining a response, in order to improve the efficiency of API debugging and testing and ensure the correctness of the API.

If you are not familiar with scripts, we recommend using this free AI tool for generation: https://app.anakin.ai/apps/21857?r=Mw4DLLke

Pre-operations

Predecessors are script code that executes before an API request and can be used to do the following:

  1. Set API Headers (request headers)

They can be used to set request headers, request bodies, validate request parameters, configure authentication and more.

  1. 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.

  1. 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.

Variable Substitution

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:

  1. 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.

  1. 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?

Post-operations

The post-operation can use the data obtained from the API response, such as status code, header, body and other information, for secondary processing:

  1. Verify that the status code and response time of the API response are as expected.
  2. Validate the content of the API response, such as JSON or XML data.
  3. Extract data from API responses and use it for subsequent requests.
  4. Automatically extract the data needed in the response.

Hierarchical Relationships

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.