Transform the response sent by the upstream server on the fly before returning
the response to the client.
The Response Transformer Advanced plugin is a superset of the
open source Response Transformer plugin, and
provides additional features:
- When transforming a JSON payload, transformations are applied to nested JSON objects and
arrays. This can be turned off and on using the
config.dots_in_keys
configuration parameter.
See Arrays and nested objects.
- Transformations can be restricted to responses with specific status codes using various
config.*.if_status
configuration parameters.
- JSON body contents can be restricted to a set of allowed properties with
config.allow.json
.
- The entire response body can be replaced using
config.replace.body
.
- Arbitrary transformation functions written in Lua can be applied.
- The plugin will decompress and recompress Gzip-compressed payloads
when the
Content-Encoding
header is gzip
.
Response Transformer Advanced includes the following additional configurations: add.if_status
, append.if_status
,
remove.if_status
, replace.body
, replace.if_status
, transform.functions
, transform.if_status
,
allow.json
, rename.if_status
, transform.json
, and dots_in_keys
.
Note on transforming bodies: Be aware of the performance of transformations on the
response body. In order to parse and modify a JSON body, the plugin needs to retain it in memory,
which might cause pressure on the worker's Lua VM when dealing with large bodies (several MBs).
Because of Nginx's internals, the Content-Length
header will not be set when transforming a response body.
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Example plugin configuration
Enable on a service
Enable on a route
Enable on a consumer
Enable globally
The following examples provide some typical configurations for enabling
the response-transformer-advanced
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one:application/json" \
--data "config.remove.headers=x-list-of-values:v1,v2,v3" \
--data "config.remove.headers=Set-Cookie:/JSESSIONID=.*/" \
--data "config.remove.headers=x-another-regex://status/$/" \
--data "config.remove.headers=x-one-more-regex:/^/begin//" \
--data "config.remove.json=json-key-toremove" \
--data "config.remove.json=another-json-key" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.json=new-json-key:some_value" \
--data "config.add.json=another-json-key:some_value" \
--data "config.add.json_types=new-json-key:string" \
--data "config.add.json_types=another-json-key:boolean" \
--data "config.add.json_types=another-json-key:number" \
--data "config.append.headers=x-existing-header:some_value" \
--data "config.append.headers=x-another-header:some_value"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: response-transformer-advanced-example
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
plugin: response-transformer-advanced
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: response-transformer-advanced-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
service: SERVICE_NAME|SERVICE_ID
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Services section, click View for the
service row.
- From the plugin section, click Add Plugin.
- Find and select the Response Transformer Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
- Click Create.
The following examples provide some typical configurations for enabling
the response-transformer-advanced
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one:application/json" \
--data "config.remove.headers=x-list-of-values:v1,v2,v3" \
--data "config.remove.headers=Set-Cookie:/JSESSIONID=.*/" \
--data "config.remove.headers=x-another-regex://status/$/" \
--data "config.remove.headers=x-one-more-regex:/^/begin//" \
--data "config.remove.json=json-key-toremove" \
--data "config.remove.json=another-json-key" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.json=new-json-key:some_value" \
--data "config.add.json=another-json-key:some_value" \
--data "config.add.json_types=new-json-key:string" \
--data "config.add.json_types=another-json-key:boolean" \
--data "config.add.json_types=another-json-key:number" \
--data "config.append.headers=x-existing-header:some_value" \
--data "config.append.headers=x-another-header:some_value"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: response-transformer-advanced-example
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
plugin: response-transformer-advanced
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: response-transformer-advanced-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
service:
name: echo
port:
number: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
route: ROUTE_NAME
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Routes from the menu, then click View for the
route row.
- From the plugin section, click Add Plugin.
- Find and select the Response Transformer Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
- Click Create.
The following examples provide some typical configurations for enabling
the response-transformer-advanced
plugin on a
consumer.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/consumers/CONSUMER_NAME|CONSUMER_ID/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one:application/json" \
--data "config.remove.headers=x-list-of-values:v1,v2,v3" \
--data "config.remove.headers=Set-Cookie:/JSESSIONID=.*/" \
--data "config.remove.headers=x-another-regex://status/$/" \
--data "config.remove.headers=x-one-more-regex:/^/begin//" \
--data "config.remove.json=json-key-toremove" \
--data "config.remove.json=another-json-key" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.json=new-json-key:some_value" \
--data "config.add.json=another-json-key:some_value" \
--data "config.add.json_types=new-json-key:string" \
--data "config.add.json_types=another-json-key:boolean" \
--data "config.add.json_types=another-json-key:number" \
--data "config.append.headers=x-existing-header:some_value" \
--data "config.append.headers=x-another-header:some_value"
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can combine consumer.id
, service.id
, or route.id
in the same request, to further narrow the scope of the plugin.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: response-transformer-advanced-example
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
plugin: response-transformer-advanced
Then, apply it to a consumer by
annotating the KongConsumer resource as follows:
apiVersion: configuration.konghq.com/v1
kind: KongConsumer
metadata:
name: CONSUMER_NAME|CONSUMER_ID
annotations:
konghq.com/plugins: response-transformer-advanced-example
kubernetes.io/ingress.class: kong
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
consumer: CONSUMER_NAME|CONSUMER_ID
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Consumers section, click View for the consumer row.
- Select the Plugins tab, then click Add Plugin.
- Find and select the Response Transformer Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the consumer ID if it is not already prefilled.
- Click Create.
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
The following examples provide some typical configurations for enabling
the response-transformer-advanced
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one:application/json" \
--data "config.remove.headers=x-list-of-values:v1,v2,v3" \
--data "config.remove.headers=Set-Cookie:/JSESSIONID=.*/" \
--data "config.remove.headers=x-another-regex://status/$/" \
--data "config.remove.headers=x-one-more-regex:/^/begin//" \
--data "config.remove.json=json-key-toremove" \
--data "config.remove.json=another-json-key" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.json=new-json-key:some_value" \
--data "config.add.json=another-json-key:some_value" \
--data "config.add.json_types=new-json-key:string" \
--data "config.add.json_types=another-json-key:boolean" \
--data "config.add.json_types=another-json-key:number" \
--data "config.append.headers=x-existing-header:some_value" \
--data "config.append.headers=x-another-header:some_value"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-response-transformer-advanced>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
plugin: response-transformer-advanced
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: response-transformer-advanced
config:
remove:
headers:
- x-toremove
- x-another-one:application/json
- x-list-of-values:v1,v2,v3
- Set-Cookie:/JSESSIONID=.*/
- x-another-regex://status/$/
- x-one-more-regex:/^/begin//
remove:
json:
- json-key-toremove
- another-json-key
add:
headers:
- x-new-header:value
- x-another-header:something
add:
json:
- new-json-key:some_value
- another-json-key:some_value
add:
json_types:
- new-json-key:string
- another-json-key:boolean
- another-json-key:number
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Plugins from the menu, then click New Plugin.
- Find and select the Response Transformer Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, set the plugin scope to Global.
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case response-transformer-advanced . |
service.name or service.id
Type: string |
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
Type: string |
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
consumer.name or consumer.id
Type: string |
The name or ID of the consumer the plugin targets.
Set one of these parameters if adding the plugin to a consumer through the top-level /plugins endpoint.
Not required if using /consumers/CONSUMER_NAME|CONSUMER_ID/plugins . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.remove.headers
optional
Type: array of string elements
|
List of headername[:value] . If only headername is given, unset the header field with the given headername . If headername:value is given, unset the header field headername when it has a specific value . If value starts and ends with a / (slash character), then it is considered to be a regular expression. Note that in accordance with RFC 7230 multiple header values with the same header name are allowed if the entire field value for that header field is defined as a comma-separated list or the header field is a Set-Cookie header field.
|
config.remove.json
optional
Type: array of string elements
|
List of property names. Remove the property from the JSON body if it is present.
|
config.remove.if_status
optional
Type: array of string elements
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.rename.headers
optional
Type: array of string elements
|
List of headername1:headername2 pairs. If a header with headername1 exists and headername2 is valid, rename header to headername2 .
|
config.rename.if_status
optional
Type: array of string elements
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.replace.headers
optional
Type: array of string elements
|
List of headername:value pairs. If and only if the header is already set, replace its old value with the new one. Ignored if the header is not already set.
|
config.replace.json
optional
Type: array of string elements
|
List of property name:value pairs. If and only if the parameter is already present, replace its old value with the new one. Ignored if the parameter is not already present.
|
config.replace.json_types
optional
Type: array of string, boolean, or number elements
|
List of JSON type names. Specify the types of the JSON values returned when replacing JSON properties.
|
config.replace.body
optional
Type: string
|
String with which to replace the entire response body.
|
config.replace.if_status
optional
Type: array of string elements
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.add.headers
optional
Type: array of string elements
|
List of headername:value pairs. If and only if the header is not already set, set a new header with the given value. Ignored if the header is already set.
|
config.add.json
optional
Type: array of string elements
|
List of name:value pairs. If and only if the property is not present, add a new property with the given value to the JSON body. Ignored if the property is already present.
|
config.add.json_types
optional
Type: array of string, boolean, or number elements
|
List of JSON type names. Specify the types of the JSON values returned when adding a new JSON property.
|
config.add.if_status
optional
Type: array of string elements
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.append.headers
optional
Type: array of string elements
|
List of headername:value pairs. If the header is not set, set it with the given value. If it is already set, a new header with the same name and the new value will be set.
|
config.append.json
optional
Type: array of string elements
|
List of name:value pairs. If the property is not present in the JSON body, add it with the given value. If it is already present, the two values (old and new) will be aggregated in an array.
|
config.append.json_types
optional
Type: array of string, boolean, or number elements
|
List of JSON type names. Specify the types of the JSON values returned when appending JSON properties.
|
config.append.if_status
optional
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.allow.json
optional
Type: array of string elements
|
Set of parameter names. Only allowed parameters are present in the JSON response body.
|
config.transform.functions
optional
Type: array of function elements
|
Set of Lua functions to perform arbitrary transforms in a response JSON body.
|
config.transform.if_status
optional
Type: array of string elements
|
List of response status codes or ranges to which the arbitrary transformation applies. Leaving empty implies that the transformations apply to all response codes.
|
config.transform.json
optional
Type: array of string elements
|
Apply Lua functions to a particular list of JSON property name or name:value pairs.
|
config.dots_in_keys
optional
Type: boolean
Default value: true
|
Whether dots (for example, customers.info.phone ) should be treated as part of a property name or used to descend into nested JSON objects. See Arrays and nested objects.
|
Note: If the value contains a ,
(comma), then the comma-separated format for lists cannot be used. The array notation must be used instead.
Order of execution
The plugin performs the response transformation in the following order:
remove –> replace –> add –> append
Arrays and nested objects
The plugin allows navigating complex JSON objects (arrays and nested objects)
when config.dots_in_keys
is set to false
(the default is true
).
array[*]
: Loops through all elements of the array.
array[N]
: Navigates to the nth element of the array (the index of the first element is 1
).
top.sub
: Navigates to the sub
property of the top
object.
These can be combined. For example, config.remove.json: customers[*].info.phone
removes
all phone
properties from inside the info
object of all entries in the customers
array.
Examples
In these examples, the plugin is enabled on a Route. This would work
similarly for Services.
- Add multiple headers by passing each
header:value
pair separately:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add multiple headers by passing comma-separated
header:value
pair:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.add.headers=h1:v1,h2:v2"
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add multiple headers passing config as a JSON body:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer-advanced", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add a body property and a header:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.add.json=p1:v1,p2=v2" \
--data "config.add.headers=h1:v1"
upstream response headers |
proxied response headers |
h1: v2 |
|
h3: v1 |
|
upstream response JSON body |
proxied response body |
{} |
{“p1” : “v1”, “p2”: “v2”} |
{“p1” : “v2”} |
{“p1” : “v2”, “p2”: “v2”} |
- Append multiple headers and remove a body property:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer-advanced", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'
upstream response headers |
proxied response headers |
h1: v1 |
|
upstream response JSON body |
proxied response body |
{“p2”: “v2”} |
{“p2”: “v2”} |
{“p1” : “v1”, “p2” : “v1”} |
{“p2”: “v2”} |
- Replace entire response body if response code is 500:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.replace.body='{\"error\": \"internal server error\"}'" \
--data "config.replace.if_status=500"
Note: The plugin doesn’t validate the value in config.replace.body
against
the content type as defined in the Content-Type
response header.
- Remove nested JSON content:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.json=customers.info.phone" \
--data "config.dots_in_keys=false"
When dots_in_keys
is false
, the customers.info.phone
value is interpreted as
nested JSON objects. When dots_in_keys
is true
(default), customers.info.phone
is
treated as a single property.
- Perform arbitrary transforms to a JSON body
Use the power of embedding Lua to perform arbitrary transformations on JSON bodies. Transformation functions
receive an argument with the JSON body, and must return the transformed response body:
-- transform.lua
-- this function transforms
-- { "foo": "something", "something": "else" }
-- into
-- { "foobar": "hello world", "something": "else" }
return function (data)
if type(data) ~= "table" then
return data
end
-- remove foo key
data["foo"] = nil
-- add a new key
data["foobar"] = "hello world"
return data
end
curl -X POST http://localhost:8001/routes/{route id}/plugins \
-F "name=response-transformer-advanced" \
-F "config.transform.functions=@transform.lua" \
-F "config.transform.if_status=200"
- Remove the entire header field with a given header name:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=h1,h2"
upstream response headers |
proxied response headers |
h1:v1,v2,v3 |
{} |
h2:v2 |
{} |
- Remove a specific header value of a given header field:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=h1:v1,h1:v2"
upstream response headers |
proxied response headers |
h1:v1,v2,v3 |
h1:v3 |
- Remove a specific header value from a comma-separated list of header values:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=h1:v1,h1:v2"
upstream response headers |
proxied response headers |
h1:v1,v2,v3 |
h1:v3 |
Note: The plugin doesn’t remove header values if the values are not separated by commas, unless it’s a Set-Cookie
header field
(as specified in RFC 7230).
- Remove a specific header value defined by a regular expression
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.remove.headers=h1:/JSESSIONID=.*/, h2://status/$/"
upstream response headers |
proxied response headers |
h1:JSESSIONID=1876832,path=/ |
h1:path=/ |
h2:/match/status/,/status/no-match/ |
h2:/status/no-match/ |
- Explicitly set the type of the added JSON value
-1
to be a number
(instead of the implicitly inferred type string
) if the response code is 500:
curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer-advanced" \
--data "config.add.json=p1:-1" \
--data "config.add.json_types=number" \
--data "config.add.if_status=500"