Skip to main content

Post-processing

Post-processing refers to any user-defined processes performed on the default endpoint output after the main API process has been executed.

tip

Since such processes are not reflected in the API information (Swagger UI) screen or openapi.json file (for OpenAPI users), there may be differences between the displayed information and the actual specifications. Therefore, we recommend that you note the custom specifications in the endpoint description.

You can access the post-processing settings for each endpoint by clicking [Post-processing] in the corresponding row:

Image from Gyazo

Post-processing settings

Post-processes are broken down into blocks. You may define any number of processing blocks, which will be executed in a series based on the order of definition.
Each block takes the results of the execution of the previous block as input. Click [Add] to append a new empty block at the end of the series.

Image from Gyazo

info

The output allow list, when executed before other processes (such as custom processing), also affects internal processes like SQL.
Since it can improve performance, it is recommended to set the output allow list at the highest level whenever possible.

Block types

Currently, Kuroco supports three types of blocks:

Block nameDescription
Allow listWhitelist filter. Define a list of fields to retain in the output - all other fields will be omitted.
Convert listOption to remove a field, rename its key, and/or to apply a function (from the list of predefined functions) to its value.
Custom functionWrite the program as a function and link it to the endpoint (similar to post-processing).

See below for the details of each type.

Allow list

The allow list is a simple field filter. A list or details API returns a lot of information, some of which you may wish to make optional or hidden from users.

You can think of it as a whitelist: only the fields you set here will be returned. Therefore, you should list all the fields used by your front-end application.

tip

For a blacklist feature instead of a whitelist, see Convert list.

Field settings

Specify the full path to the field in the JSON output, starting from the root. Skip over arrays and only specify objects - the filter will be applied to all objects in an array.

tip

If your allow list does not specify any field or contains an error in the path name, the endpoint will output an empty JSON object.

Example scenario

Original Output
{
"list": [
{
"subject": "First article",
"contents": "The contents of the first article",
"sponsors": ["Company X"]
},
{
"subject": "Second article",
"contents": "The contents of the second article",
"sponsors": ["Company Y"]
}
],
"pageInfo": {
"totalCnt": 7,
"pageNo": 1,
...
}
}

For example, in the above sample code, we would like to remove sponsors only.

The three fields we are keeping, including all root and child fields, are:

  • list.subject
  • list.contents
  • pageInfo

Your configuration should look like this:

Image from Gyazo

tip

Make sure to denote nested JSON objects using . and ignore all arrays.

The drawback to this simple filter is a lack of flexibility. If you need more in-depth customization, see Convert list and Custom function.

Convert list

This block allows you to add processes to each field by choosing among available options.

Click [Add field] to add a sub-block defining a process for a specific field. Note the two different [Delete] functions in this block: the one to the right (a) deletes the corresponding sub-block row, while the one at the bottom (b) deletes the entire block.

Image from Gyazo

Field settings

FieldDescription
FieldSpecify the field path in the same way as for allow lists, using dot-separated nested objects and skipping over lists. The filter will be applied to all elements in a list.
  • Note: If you enter a non-existent field, a new field with this name will be created under the specified parent. Therefore, you can use convert lists to easily add custom static data to your API.
Remove/copy field
  • Remove: Enabled this option to remove this field from the response. All other transformations for this sub-block row will be disabled.
  • Select this option and fill in "New name" to create a new field with the specified process. Note: The original field with its original value will be preserved.
New nameSpecify the new name of the field. A field with the new name will be added to the same parent object.
  • Note: This is NOT a path, and you cannot use dot notation to set a different nested structure from the original field. For example, an identity transformation of a.b.c.d would simply be d, and you do not need to specify a new name in this case.
ProcessSelect one or more optional transformation(s) to apply to the field. In cases of multiple transformations, they will be applied in the order of selection. The specific actions of each process are listed below.
  • To remove a process, click [-] next to it.
    Image from Gyazo
DeleteRemove the current processing sub-block.

Transformation processes

Process nameAction(s)
NullCopy the original value as is (no transformation).
TruncateTruncate the value to a maximum of N characters. Specify the length in the input box on the right.
  • Warning: If left blank (or set to 0), the max length will default to 10.
TrimRemove all spaces/tabs from the start and the end of the string.
  • Warning: This function will convert the value into string format.
StrtotimeApply the strtotime PHP function to the value.
Date formatApply the date PHP function to the value to format it as a Unix timestamp.
Locale Date FormatApply the specialized pg_dateformat function to the value.
UppercaseCast every character to its uppercase variant, if any.
LowercaseCast every character to its lowercase variant, if any.
SprintfApply the sprintf PHP function to the input. The existing value is used as a second argument, which you can access using %s or other specifiers.
Nl2brInsert <br /> before all new lines (\n, \r and \r\n) in a string. Useful when rendering to html.
FileSizeRetrieve the bytesize of a file if it exists at the specified path.
ImageSizeRetrieve the bytesize of an image if it exists at the specified path.

Example scenario

Consider the same sample code as before:

Original Output
{
"list": [
{
"subject": "First article",
"contents": "The contents of the first article",
"sponsors": ["Company X"]
},
{
"subject": "Second article",
"contents": "The contents of the second article",
"sponsors": ["Company Y"]
}
],
"pageInfo": {
"totalCnt": 7,
"pageNo": 1,
...
}
}

Suppose you want to add a new field called title, which contains the uppercase version of the contents of subject. Configure your field settings as follows:

FieldDescription
Fieldlist.subject
Remove/copy fieldSelect "Copy".
New nametitle
ProcessUppercase

Your configuration should look like this:

Image from Gyazo New output:

Filtered Output
{
"list":
[
{
"subject": "First article",
"contents": "The contents of the first article",
"sponsors": [
"Company X"
],
"title": "FIRST ARTICLE"
},
{
"subject": "Second article",
"contents": "The contents of the second article",
"sponsors": [
"Company Y"
],
"title": "SECOND ARTICLE"
}
],
"pageInfo":
{
"totalCnt": 7,
"pageNo": 1
}
}

Custom function

If your API requires more in-depth features, write a custom function defining how your data should be transformed and link it to your endpoint.

Custom function creation

Please create a custom process to be used in post-processing, referring to the custom processing as a reference.

Image from Gyazo To avoid confusion with custom functions used in other modules, we recommend setting it up as follows:

  • Create a custom function category dedicated to post-processing in advance and set it as the category of the function.
  • Match the title of the custom function with the path name of the endpoint.

Linking the function to the target endpoint

  1. In the left sidebar menu, click [API] and select the target endpoint list screen.
  2. Select the target endpoint from the list and click [Post-processing].
  3. In the dropdown list, select [Custom function] and choose the correct category and function name (list).
  4. Click [Save].
caution

Double check to make sure you have linked the function correctly. If you made a mistake, the endpoint may not work as expected.

Example configuration:

Image (fetched from Gyazo)

Post-processing variables

The variables used in post-processing contain reserved words with specific meanings. You can customize the endpoint behavior using these variables.

VariableTypeExplanation
$jsonAvailableContains the original JSON output of the API.
$processed_jsonReturnAssign this variable and include the modified JSON response to overwrite the original.

When in trouble

If a post-process is not working as expected, verify the following:

  • The post-processing custom function has been linked properly.
  • You have linked the correct custom function.
  • The variable name is correct.
  • The variable data is stored in the correct format.
  • The custom function contains no syntax errors.

Support

If you have any other questions, please contact us or check out Our Slack Community.