API result filters
The default response structure of an API may not be suitable for every scenario. Filters allow you to transform the original response structure. This reduces the cost of API integration and custom development.
Usage
After you configure a filter, it automatically transforms the format of the response.
Example result
The original response structure of an API is as follows:
-
code: The response status code. The value can be SUCCESS or ERROR.
-
data: The returned data. The structure can vary based on the data source type. It is usually an ArrayList or a jsonObject structure.
-
traceId: The unique ID for tracing this API call.
-
message: The reason for the error if the code is ERROR.
An API user wants to remove the encapsulation of fields such as `code` and `traceId` and return only the `data` section. After the filter is configured, the response structure is as follows:
The response now contains only the content from the `data` field. No manual transformation is needed.
Configuration methods
Configure a filter during API development
You can configure a filter during API development. After the filter is configured, it applies to all API calls.
How to use:
When you create or edit an API, go to the third step, the API Configuration and Testing page. In the upper-left corner of the Response Content section, click the Filter button to enable the filter.
You can set up filters in two ways:
-
Format processing: Process the response using specified methods. You can filter the `code`, `message`, and `traceId` fields, or return only the `data` content. You can select multiple options.
-
Custom logic: Use a custom script to transform the entire response data structure. JavaScript (JS) functions are currently supported. Support for more scripting languages will be added in a future release.
The following is an example of a JS function script for custom logic:
function filter(response) {
// Get the first data set from response.data.
var data = response.data[0];
// Concatenate the certificate number and customer name, then return the result.
return data.zzhm + " : " + data.khmc;
}
The result of the transformation using the script above is:
The filter takes effect after you configure it. To disable the filter, turn off the Filter button.
Configure a filter for an authorized API
For an authorized API, you can configure a separate filter. This filter takes effect only when the currently authorized application calls the API. It does not affect the API's response structure or calls to the API from other applications.
How to use:
On the Authorized APIs page, find the desired API. In the Actions column, click Test to open the test page for that authorized API.
On the Test page for the authorized API, click the Filter button to configure the filter. The configuration method is the same as that for filters configured during API development.
If an API already has a development filter, and you add an authorization filter, both filters are applied in sequence. The authorization filter processes the output from the development filter. The final result is then returned to the caller.