Transform the response sent by the upstream server on the fly before returning the response to the client.
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.
For additional response transformation features, check out the
Response Transformer Advanced plugin. Response Transformer
Advanced adds the following abilities:
- 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 Response Transformer Advanced 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
.
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
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=response-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--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-example
config:
remove:
headers:
- x-toremove
- x-another-one
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
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-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
service: SERVICE_NAME|SERVICE_ID
config:
remove:
headers:
- x-toremove
- x-another-one
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 Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- In the Plugins section, click Add Plugin.
- Find and select the Response Transformer plugin.
- Click Create.
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 plugin.
- 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
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=response-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--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-example
config:
remove:
headers:
- x-toremove
- x-another-one
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
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-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
route: ROUTE_NAME
config:
remove:
headers:
- x-toremove
- x-another-one
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 Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- Select a route.
- In the Plugins section, click Add Plugin.
- Find and select the Response Transformer plugin.
- Click Create.
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 plugin.
- 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
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" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--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-example
config:
remove:
headers:
- x-toremove
- x-another-one
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
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-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
consumer: CONSUMER_NAME|CONSUMER_ID
config:
remove:
headers:
- x-toremove
- x-another-one
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 plugin.
- 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
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=response-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--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>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
remove:
headers:
- x-toremove
- x-another-one
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
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: response-transformer
config:
remove:
headers:
- x-toremove
- x-another-one
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 plugin.
- 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 . |
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 header names. Unset the header(s) with the given name.
|
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.rename.headers
optional
Type: array of string elements
|
List of original_header_name:new_header_name pairs. If the header original_headername is already set, rename it to new_headername . Ignored if the header is not already set.
|
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: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 elements
|
List of JSON type names. Specify the types of the JSON values returned when
replacing JSON properties. Each string
element can be one of: boolean, number, or string.
|
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 property: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 elements
|
List of JSON type names. Specify the types of the JSON values returned when adding
a new JSON property. Each string element can be one of: boolean, number, or string.
|
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. Each string
element can be one of: boolean, number, or string.
|
config.append.json
optional
Type: array of string elements
|
List of property: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 elements
|
List of JSON type names. Specify the types of the JSON values returned when appending
JSON properties. Each string element can be one of: boolean, number, or string.
|
Note: if the value contains a ,
then the comma separated format for lists cannot be used. The array notation must be used instead.
Order of execution
Plugin performs the response transformation in following order
remove –> rename –> replace –> add –> append
Examples
In these examples we have the plugin enabled on a Route. This would work
similar for Services.
- Add multiple headers by passing each header:value pair separately:
With a database
Without a database
curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=response-transformer" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
plugins:
- name: response-transformer
route: {route}
config:
add:
headers: ["h1:v1", "h2:v2"]
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add multiple headers by passing comma separated header:value pair (only possible with a database):
curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=response-transformer" \
--data "config.add.headers=h1:v1,h2:v2"
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add multiple headers passing config as JSON body (only possible with a database):
curl -X POST http://localhost:8001/routes/{route}/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'
upstream response headers |
proxied response headers |
h1: v1 |
|
- Add a body property and a header:
With a database
Without a database
curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=response-transformer" \
--data "config.add.json=p1:v1,p2=v2" \
--data "config.add.headers=h1:v1"
plugins:
- name: response-transformer
route: {route}
config:
add:
json: ["p1:v1", "p2=v2"]
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:
With a database
Without a database
curl -X POST http://localhost:8001/routes/{route}/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'
plugins:
- name: response-transformer
route: {route}
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”} |
- 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}/plugins \
--data "name=response-transformer" \
--data "config.add.json=p1:-1" \
--data "config.add.json_types=number"