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 Transformed 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
Enable the plugin on a service
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a service by
making the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/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"
SERVICE
is 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}
labels:
app: {SERVICE}
annotations:
konghq.com/plugins: <response-transformer-example>
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: {SERVICE}
selector:
app: {SERVICE}
{SERVICE}
is 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
.
For example, configure this plugin on a service by
adding this section to your declarative configuration file:
plugins:
- name: response-transformer
service: {SERVICE}
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
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
Configure this plugin on a service:
- In Konnect Cloud, select the service on the ServiceHub page.
- Scroll down to Versions and select the version.
- Scroll down to Plugins and click New Plugin.
- Find and select the Response Transformer plugin.
- Click Create.
Configure this plugin on a service:
- In Kong Manager, select the workspace.
- From the Dashboard, scroll down to Services and click View for the
service row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the Response Transformer 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 prefilled.
- Click Create.
Enable the plugin on a route
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a route with:
$ curl -X POST http://{HOST}:8001/routes/{ROUTE}/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"
ROUTE
is 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/v1beta1
kind: Ingress
metadata:
name: {ROUTE}
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: <response-transformer-example>
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
serviceName: echo
servicePort: 80
ROUTE
is 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
.
For example, configure this plugin on a route by
adding this section to your declarative configuration file:
plugins:
- name: response-transformer
route: <route>
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
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Configure this plugin on a route:
- In Konnect Cloud, select the service from the ServiceHub page.
- Scroll down to Versions and select the version.
- Select the route.
- Scroll down to Plugins and click Add Plugin.
- Find and select the Response Transformer plugin.
- Click Create.
Configure this plugin on a route:
- In Kong Manager, select the workspace.
- From the Dashboard, select Routes in the left navigation.
- Click View for the route row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the Response Transformer 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.
Enabling the plugin on a consumer
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
For example, configure this plugin on a consumer with:
$ curl -X POST http://{HOST}:8001/consumers/{CONSUMER}/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"
CONSUMER
is the id
or username
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}
annotations:
konghq.com/plugins: <response-transformer-example>
kubernetes.io/ingress.class: kong
CONSUMER
is the id
or username
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
.
For example, configure this plugin on a consumer by
adding this section to your declarative configuration file:
plugins:
- name: response-transformer
consumer: {CONSUMER}
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
CONSUMER
is the id
or username
of the consumer that this plugin
configuration will target.
Configure this plugin on a consumer:
- In Kong Manager, select the workspace.
- From the Dashboard, scroll down to Consumers and click View for the consumer row.
- Select the Plugins tab.
- Click Add Plugin.
-
Find and select the Response Transformer 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 Global.
- Click Create.
Enable the plugin globally
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.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
For example, configure this plugin globally with:
$ curl -X POST http://{HOST}: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
For example, configure this plugin using the 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
Configure this plugin globally:
- In Kong Manager, select the workspace.
- From the Dashboard, select Plugins in the left navigation.
- Click New Plugin.
-
Find and select the Response Transformer 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 . |
service.id
Type: string |
The ID of the Service the plugin targets. |
route.id
Type: string |
The ID of the Route the plugin targets. |
consumer.id
Type: string |
The ID of the Consumer the plugin targets. |
enabled
required
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"