Transform the response sent by the upstream server on the fly on Kong, 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.
Configuration Reference
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, x-another-one" \
--data "config.remove.json=json-key-toremove, another-json-key" \
--data "config.add.headers=x-new-header:value,x-another-header:something" \
--data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
--data "config.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.
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
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
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, x-another-one" \
--data "config.remove.json=json-key-toremove, another-json-key" \
--data "config.add.headers=x-new-header:value,x-another-header:something" \
--data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
--data "config.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.
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
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_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:
serviceName: echo
servicePort: 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
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, x-another-one" \
--data "config.remove.json=json-key-toremove, another-json-key" \
--data "config.add.headers=x-new-header:value,x-another-header:something" \
--data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
--data "config.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 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
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
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, x-another-one" \
--data "config.remove.json=json-key-toremove, another-json-key" \
--data "config.add.headers=x-new-header:value,x-another-header:something" \
--data "config.add.json=new-json-key:some_value, another-json-key:some_value" \
--data "config.append.headers=x-existing-header:some_value, 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
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
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. |
api_id
Type: string |
The ID of the API the plugin targets.
Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32. |
config.remove.headers
optional
|
List of header names. Unset the header(s) with the given name.
|
config.remove.json
optional
|
List of property names. Remove the property from the JSON body if it is present.
|
config.replace.headers
optional
|
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
|
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.add.headers
optional
|
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
|
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.append.headers
optional
|
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
|
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.
|
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 –> replace –> add –> append
Examples
In these examples we have the plugin enabled on a Route. This would work
similar for Services, or the depreciated API entity.
- Add multiple headers by passing each header:value pair separately:
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
upstream response headers | proxied response headers
— | —
h1: v1 | <ul><li>h1: v1</li><li>h2: v1</li></ul>
—
- Add multiple headers by passing comma separated header:value pair:
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer" \
--data "config.add.headers=h1:v1,h2:v2"
upstream response headers | proxied response headers
— | —
h1: v1 | <ul><li>h1: v1</li><li>h2: v1</li></ul>
—
- Add multiple headers passing config as JSON body:
$ curl -X POST http://localhost:8001/routes/{route id}/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 | <ul><li>h1: v1</li><li>h2: v1</li></ul>
—
- Add a body property and a header:
$ curl -X POST http://localhost:8001/routes/{route id}/plugins \
--data "name=response-transformer" \
--data "config.add.json=p1:v1,p2=v2" \
--data "config.add.headers=h1:v1"
upstream response headers |
proxied response headers: |
h1: v2 |
<ul><li>h1: v2</li><li>h2: v1</li></ul> |
h3: v1 |
<ul><li>h1: v1</li><li>h2: v1</li><li>h3: v1</li></ul> |
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", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'
upstream response headers |
proxied response headers |
h1: v1 |
<ul><li>h1: v1</li><li>h1: v2</li><li>h2: v1</li></ul> |
upstream response JSON body |
proxied response body |
{“p2”: “v2”} |
{“p2”: “v2”} |
{“p1” : “v1”, “p2” : “v1”} |
{“p2”: “v2”} |