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-advanced
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/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.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-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
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}
labels:
app: {SERVICE}
annotations:
konghq.com/plugins: <response-transformer-advanced-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
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
service: {SERVICE}
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
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.
- 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 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 prefilled.
- 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://{HOST}:8001/routes/{ROUTE}/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.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-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
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/v1beta1
kind: Ingress
metadata:
name: {ROUTE}
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: <response-transformer-advanced-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
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
route: <route>
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
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.
- 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 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://{HOST}:8001/consumers/{CONSUMER}/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.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-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
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}
annotations:
konghq.com/plugins: <response-transformer-advanced-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
.
Add this section to your declarative configuration file:
plugins:
- name: response-transformer-advanced
consumer: {CONSUMER}
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
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.
- 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 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 Global.
- 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://{HOST}: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.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
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
append:
headers:
- x-existing-header:some_value
- x-another-header:some_value
- In Kong Manager, select the workspace.
- From the Dashboard, select Plugins in the left navigation.
- 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.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
|
List of header_name[:header_value]. If only header_name is given, unset the header field with the given header_name. If header_name:header_value is given, remove a specific header_value. If header_value starts and ends with a ‘/’ (slash character), then it is considered to be a regular expression. Note that as per https://httpwg.org/specs/rfc7230.html#field.order 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
|
List of property names. Remove the property from the JSON body if it is present.
|
config.remove.if_status
optional
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes.
|
config.rename.headers
optional
|
List of headername:value pairs. Renames header with headername to new name.
|
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.replace.body
optional
|
String with which to replace the entire response body
|
config.replace.if_status
optional
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes
|
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.add.if_status
optional
|
List of response status codes or status code ranges to which the transformation will apply. Empty means all response codes
|
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.
|
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.whitelist.json
optional
|
Set of parameter names. Only allowed parameters are present in the JSON response body.
|
config.transform.functions
optional
|
Set of Lua functions to perform arbitrary transforms in a response JSON body.
|
config.transform.if_status
optional
|
List of response status codes or ranges to which the arbitrary transformation applies. Leaving empty implies that the transformations apply to all response codes.
|
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.
- 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 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.
- 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 a 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. RFC reference: https://httpwg.org/specs/rfc7230.html#field.order
- 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/ |