Function Description
The transformer
plugin can transform request/response headers, request query parameters, and request/response body parameters. Supported transformation operation types include deletion, renaming, updating, adding, appending, mapping, and deduplication.
Execution Attributes
Plugin execution phase: authentication phase
Plugin execution priority: 410
Configuration Fields
Name | Data Type | Fill Requirement | Default Value | Description |
---|---|---|---|---|
reqRules | string | Optional, at least one of reqRules or respRules must be filled | - | Request transformer configuration, specifying the transformation operation type and rules for transforming request headers, request query parameters, and request body |
respRules | string | Optional, at least one of reqRules or respRules must be filled | - | Response transformer configuration, specifying the transformation operation type and rules for transforming response headers and response body |
The configuration fields for each item in reqRules
and respRules
are as follows:
Name | Data Type | Fill Requirement | Default Value | Description |
---|---|---|---|---|
operate | string | Required, optional values are remove , rename , replace , add , append , map , dedupe | - | Specifies the transformation operation type. Supported operation types include remove (remove), rename (rename), replace (replace), add (add), append (append), map (map), dedupe (dedupe). When there are multiple transformation rules of different types, they are executed in the order of the above operation types. |
mapSource | string | Optional, optional values are headers , querys , body | - | Valid only when operate is map . Specifies the mapping source. If this field is not filled, the default mapping source is itself. |
headers | array of object | Optional | - | Specifies transformation rules for request/response headers. |
querys | array of object | Optional | - | Specifies transformation rules for request query parameters. |
body | array of object | Optional | - | Specifies transformation rules for request/response body parameters. Request body transformations allow content-types of application/json , application/x-www-form-urlencoded , and multipart/form-data while response body transformations only allow content-type of application/json . |
The configuration fields for each item in headers
, querys
, body
are as follows:
Name | Data Type | Fill Requirement | Default Value | Description |
---|---|---|---|---|
key | string | Optional | - | Used when operate is remove , see Transformation Operation Types for details. |
oldKey | string | Optional | - | Used when operate is rename , see Transformation Operation Types for details. |
newKey | string | Optional | - | Used when operate is rename , see Transformation Operation Types for details. |
key | string | Optional | - | Used when operate is replace , see Transformation Operation Types for details. |
newValue | string | Optional | - | Used when operate is replace , see Transformation Operation Types for details. |
key | string | Optional | - | Used when operate is add , see Transformation Operation Types for details. |
value | string | Optional | - | Used when operate is add , see Transformation Operation Types for details. |
key | string | Optional | - | Used when operate is append , see Transformation Operation Types for details. |
appendValue | string | Optional | - | Used when operate is append , see Transformation Operation Types for details. |
fromKey | string | Optional | - | Used when operate is map , see Transformation Operation Types for details. |
toKey | string | Optional | - | Used when operate is map , see Transformation Operation Types for details. |
key | string | Optional | - | Used when operate is dedupe , see Transformation Operation Types for details. |
strategy | string | Optional | - | Used when operate is dedupe , see Transformation Operation Types for details. |
value_type | string | Optional, optional values are object , boolean , number , string | string | When content-type: application/json , this field specifies the value type of request/response body parameters. |
host_pattern | string | Optional | - | Specifies the request hostname matching rule. Valid when transformation operation type is replace , add , append . |
path_pattern | string | Optional | - | Specifies the request path matching rule. Valid when transformation operation type is replace , add , append . |
Note:
request transformer
supports the following transformation objects: request headers, request query parameters, request body (application/json, application/x-www-form-urlencoded, multipart/form-data).response transformer
supports the following transformation objects: response headers, response body (application/json).- The plugin supports bidirectional conversion capability, meaning that a single plugin can perform transformations on both requests and responses.
- The execution order of transformation operation types is the order written in the configuration file, e.g., remove → rename → replace → add → append → map → dedupe or dedupe → map → append → add → replace → rename → remove.
- When the transformation object is headers,
key
is case-insensitive. When headers are operated and arerename
ormap
,value
is also case-insensitive (as this field has a key meaning). However,key
andvalue
fields in querys and body are case-sensitive. value_type
is only effective for content type application/json for request/response bodies.host_pattern
andpath_pattern
support RE2 syntax, valid only forreplace
,add
,append
operations. In a transformation rule, only one of the two can be optionally filled. If both are filled, thenhost_pattern
takes effect whilepath_pattern
becomes ineffective.
Transformation Operation Types
Operation Type | Key Field Meaning | Value Field Meaning | Description |
---|---|---|---|
Remove remove | Target key | Not required | If the specified key exists, delete it; otherwise, no operation |
Rename rename | Target oldKey | New key name newKey | If the specified oldKey:value exists, rename its key to newKey , resulting in newKey:value ; otherwise, no operation |
Replace replace | Target key | New value newValue | If the specified key:value exists, update its value to newValue , resulting in key:newValue ; otherwise, no operation |
Add add | Added key | Added value | If the specified key:value does not exist, add it; otherwise, no operation |
Append append | Target key | Appending value appendValue | If the specified key:value exists, append appendValue to get key:[value…, appendValue] ; otherwise, it is equivalent to performing add operation, resulting in key:appendValue . |
Map map | Mapping source fromKey | Mapping target toKey | If the specified fromKey:fromValue exists, map its value fromValue to the value of toKey, resulting in toKey:fromValue , while retaining fromKey:fromValue (note: if toKey already exists, its value will be overwritten); otherwise, no operation. |
Deduplicate dedupe | Target key | Specified deduplication strategy strategy | strategy optional values include:RETAIN_UNIQUE : Retain all unique values in order, e.g., k1:[v1,v2,v3,v3,v2,v1] , deduplication results in k1:[v1,v2,v3] .RETAIN_LAST : Retain the last value, e.g., k1:[v1,v2,v3] , deduplication results in k1:v3 .RETAIN_FIRST (default): Retain the first value, e.g., k1:[v1,v2,v3] , deduplication results in k1:v1 .(Note: When deduplication results in only one element v1, the key-value pair becomes k1:v1 , not k1:[v1] .) |
Configuration Example
Implement Routing Based on Body Parameters
Configuration example:
This rule extracts the userId
from the request body and sets it in the request header x-user-id
. This allows routing based on body parameters using Higress’s ability to match on request headers.
This configuration supports both application/json
and application/x-www-form-urlencoded
types of request bodies.
For example: For application/json type body
The value of the userId
field will be extracted from the JSON and set to x-user-id
. The backend service will receive a request header with: x-user-id: 12
.
After the plugin adds this header, the gateway will recalculate the routes, allowing the routing configuration to match the specific target service based on this request header.
For application/x-www-form-urlencoded type body
The value of the userId
field will be extracted from the form format k1=v1&k2=v2
and set to x-user-id
. The backend service will receive a request header with: x-user-id: 12
.
After the plugin adds this header, the gateway will recalculate the routes, allowing the routing configuration to match the specific target service based on this request header.
JSON Path Support
You can extract fields from complex JSON according to GJSON Path syntax.
Common operations include, for the following JSON:
You can achieve such extractions:
Now, if you want to extract the first
field from the second item in friends
from the above JSON formatted body and set it to the header x-first-name
, while also extracting the last
field to set it to the header x-last-name
, you can use this plugin configuration:
Request Transformer
Transforming Request Headers
Send Request
Transforming Request Query Parameters
Send Request
Transforming Request Body
Send Requests: 1. Content-Type: application/json
2. Content-Type: application/x-www-form-urlencoded
3. Content-Type: multipart/form-data
Response Transformer
Similar to Request Transformer, this only describes the precautions for transforming JSON-formatted request/response bodies:
Key Nesting .
- In general, a key containing
.
indicates a nested meaning, as follows:
- When using
\.
to escape.
in the key, it indicates a non-nested meaning, as follows:
When enclosing a string with double quotes, use
\\.
for escaping
Accessing Array Elements .index
You can access array elements by their index array.index
, where the index starts from 0:
- Remove the first element of
user
:
- Rename the key
123
of the first element ofusers
tomsg
:
Iterating Array Elements .#
You can use array.#
to iterate over an array:
❗️This operation can only be used in replace, do not attempt this operation in other transformations to avoid unpredictable results