Skip to main content

Pre-function

Overview

The Pre-function policy allows users to execute custom scripts before a request is processed. This policy enables users to modify requests dynamically by adding, removing, or transforming request parameters, headers, and body content.

The Pre-function policy is commonly used for:

  • Data Masking: Obscuring sensitive data such as personal identification numbers before they reach the backend.
  • Query String Manipulation: Modifying or filtering query parameters dynamically.
  • Token Validation: Implementing authentication and security logic.
  • Routing Control: Modifying routing decisions based on request attributes.

Configuration Details

The Pre-function policy consists of a code editor where users write scripts. The interface includes the following components:

Code Editor

The editor is the primary area where users can write and modify custom Lua scripts for different execution phases of the API request lifecycle. There are four tabs available within the code editor:

  • Access: Code entered here is executed during the access phase, allowing users to manipulate request-level logic such as authentication, headers, path rewrites, etc.
  • Header Filter: Used for modifying response headers before they are sent back to the client.
  • Body Filter: Enables manipulation of the response body, useful for transforming or masking response content.
  • Log: Used to implement logging behaviors based on request or response data after the request has been processed.

Each tab contains a dedicated code editor and access to Snippets and Examples that help speed up development.

note

Code must be written and saved separately within each tab if multiple execution phases are to be utilized.

Pre-function Code Editor

Snippets (Predefined Code Blocks)

The Snippets section provides predefined code blocks that users can insert into the editor. These help users quickly implement commonly used request modifications without needing to write code from scratch.

List of snippets:

SnippetsDescription
JSON ParserLoads and processes JSON-formatted request bodies.
Get Request Raw BodyRetrieves the entire request body before processing.
Set Service Request Raw BodyModifies the request body before forwarding it to the backend.
Get Request MethodRetrieves the HTTP method (GET, POST, etc.).
Get Request PathObtains the full request path.
Get Request HeaderExtracts values from specific request headers.
Set Request HeaderModifies or adds request headers dynamically.
Set Response ExitTerminates a request and returns a custom response.
Set Service HostChanges the destination server dynamically.
Set Service Request SchemeModifies the request scheme (http or https).
Set Service Request PathAlters the request path before sending it to the backend.

Pre-function Snippets

Examples Section

This section contains predefined example scripts that showcase practical implementations of the Pre-function policy. These examples serve as templates for users to quickly implement common scenarios.

List of examples:

ExamplesDescription
Masking DataMasks sensitive information in request payloads. Replaces Korean Resident Registration Number when it appears as a value in a query string with “SSN-DATA” and continue routing.
Query String ManipulationRemove all query strings except for the "service" key and its corresponding value.
Filters parameters based on backend routing conditions:
- When the routing target backend address is A, all query parameters will be completely removed.
- However, if the routing target backend address is B, only a specific set of query parameters will be permitted; any others will be discarded.
Token ValidationImplement custom validation for the "Authorization: bearer xxxxxxxx" token using regular expressions. AND, specific Method + Path combinations do not perform bearer token validation.
RoutingAllow the user to code so that they can continue routing or immediately handle failure based on the situation.

Pre-function Examples

Example to apply

Users can modify scripts directly in the code editor or use predefined snippets. Clicking on an example opens a popup editor, allowing further customization.