The Request Transformer plugin for Kong allows simple transformation of requests
before they reach the upstream server. These transformations can be simple substitutions
or complex ones matching portions of incoming requests using regular expressions, saving
those matched strings into variables, and substituting those strings into transformed requests using flexible templates.
For additional request transformation features, check out the
Request Transformer Advanced plugin.
With the advanced plugin, you can also limit the list of allowed parameters in the request 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 request-transformer
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=request-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--data "config.remove.querystring=qs-old-name:qs-new-name" \
--data "config.remove.querystring=qs2-old-name:qs2-new-name" \
--data "config.remove.body=formparam-toremove" \
--data "config.remove.body=formparam-another-one" \
--data "config.replace.body=body-param1:new-value-1" \
--data "config.replace.body=body-param2:new-value-2" \
--data "config.rename.headers=header-old-name:header-new-name" \
--data "config.rename.headers=another-old-name:another-new-name" \
--data "config.rename.querystring=qs-old-name:qs-new-name" \
--data "config.rename.querystring=qs2-old-name:qs2-new-name" \
--data "config.rename.body=param-old:param-new" \
--data "config.rename.body=param2-old:param2-new" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.querystring=new-param:some_value" \
--data "config.add.querystring=another-param:some_value" \
--data "config.add.body=new-form-param:some_value" \
--data "config.add.body=another-form-param: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: request-transformer-example
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param:some_value
plugin: request-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: request-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: request-transformer
service: SERVICE_NAME|SERVICE_ID
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param: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 Request 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 request-transformer
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=request-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--data "config.remove.querystring=qs-old-name:qs-new-name" \
--data "config.remove.querystring=qs2-old-name:qs2-new-name" \
--data "config.remove.body=formparam-toremove" \
--data "config.remove.body=formparam-another-one" \
--data "config.replace.body=body-param1:new-value-1" \
--data "config.replace.body=body-param2:new-value-2" \
--data "config.rename.headers=header-old-name:header-new-name" \
--data "config.rename.headers=another-old-name:another-new-name" \
--data "config.rename.querystring=qs-old-name:qs-new-name" \
--data "config.rename.querystring=qs2-old-name:qs2-new-name" \
--data "config.rename.body=param-old:param-new" \
--data "config.rename.body=param2-old:param2-new" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.querystring=new-param:some_value" \
--data "config.add.querystring=another-param:some_value" \
--data "config.add.body=new-form-param:some_value" \
--data "config.add.body=another-form-param: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: request-transformer-example
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param:some_value
plugin: request-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: request-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: request-transformer
route: ROUTE_NAME
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param: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 Request 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 request-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=request-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--data "config.remove.querystring=qs-old-name:qs-new-name" \
--data "config.remove.querystring=qs2-old-name:qs2-new-name" \
--data "config.remove.body=formparam-toremove" \
--data "config.remove.body=formparam-another-one" \
--data "config.replace.body=body-param1:new-value-1" \
--data "config.replace.body=body-param2:new-value-2" \
--data "config.rename.headers=header-old-name:header-new-name" \
--data "config.rename.headers=another-old-name:another-new-name" \
--data "config.rename.querystring=qs-old-name:qs-new-name" \
--data "config.rename.querystring=qs2-old-name:qs2-new-name" \
--data "config.rename.body=param-old:param-new" \
--data "config.rename.body=param2-old:param2-new" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.querystring=new-param:some_value" \
--data "config.add.querystring=another-param:some_value" \
--data "config.add.body=new-form-param:some_value" \
--data "config.add.body=another-form-param: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: request-transformer-example
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param:some_value
plugin: request-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: request-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: request-transformer
consumer: CONSUMER_NAME|CONSUMER_ID
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param: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 Request 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 request-transformer
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=request-transformer" \
--data "config.remove.headers=x-toremove" \
--data "config.remove.headers=x-another-one" \
--data "config.remove.querystring=qs-old-name:qs-new-name" \
--data "config.remove.querystring=qs2-old-name:qs2-new-name" \
--data "config.remove.body=formparam-toremove" \
--data "config.remove.body=formparam-another-one" \
--data "config.replace.body=body-param1:new-value-1" \
--data "config.replace.body=body-param2:new-value-2" \
--data "config.rename.headers=header-old-name:header-new-name" \
--data "config.rename.headers=another-old-name:another-new-name" \
--data "config.rename.querystring=qs-old-name:qs-new-name" \
--data "config.rename.querystring=qs2-old-name:qs2-new-name" \
--data "config.rename.body=param-old:param-new" \
--data "config.rename.body=param2-old:param2-new" \
--data "config.add.headers=x-new-header:value" \
--data "config.add.headers=x-another-header:something" \
--data "config.add.querystring=new-param:some_value" \
--data "config.add.querystring=another-param:some_value" \
--data "config.add.body=new-form-param:some_value" \
--data "config.add.body=another-form-param:some_value"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-request-transformer>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param:some_value
plugin: request-transformer
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: request-transformer
config:
remove:
headers:
- x-toremove
- x-another-one
remove:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
remove:
body:
- formparam-toremove
- formparam-another-one
replace:
body:
- body-param1:new-value-1
- body-param2:new-value-2
rename:
headers:
- header-old-name:header-new-name
- another-old-name:another-new-name
rename:
querystring:
- qs-old-name:qs-new-name
- qs2-old-name:qs2-new-name
rename:
body:
- param-old:param-new
- param2-old:param2-new
add:
headers:
- x-new-header:value
- x-another-header:something
add:
querystring:
- new-param:some_value
- another-param:some_value
add:
body:
- new-form-param:some_value
- another-form-param: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 Request 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 request-transformer . |
instance_name
optional
Type: string
|
An optional custom name to identify an instance of the plugin, for example request-transformer_my-service .
Useful when running the same plugin in multiple contexts, for example, on multiple services.
|
service.name or service.id
optional
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
optional
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
optional
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
optional
Type: boolean
Default value: true
|
Whether this plugin will be applied. |
config.http_method
optional
Type: string
|
Sets the HTTP method for the upstream request.
|
config.remove.headers
optional
Type: array of string elements
|
List of header names. Unset the headers with the given name.
|
config.remove.querystring
optional
Type: array of string elements
|
List of querystring names. Remove the querystring if it is present.
|
config.remove.body
optional
Type: array of string elements
|
List of parameter names. Remove the parameter if and only if content-type is one the following:
[application/json , multipart/form-data , application/x-www-form-urlencoded ] and the parameter is present.
|
config.replace.uri
optional
Type: string
|
Updates the upstream request URI with a given value. This value can be used to update
only the path part of the URI, not the scheme or the hostname.
|
config.replace.body
optional
Type: array of string elements
|
List of paramname:value pairs. If and only if content-type is one the following
[application/json , multipart/form-data , application/x-www-form-urlencoded ] and the
parameter is already present, replace its old value with the new one. Ignored if
the parameter is not already present.
|
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.querystring
optional
Type: array of string elements
|
List of queryname:value pairs . If and only if the field name is already set,
replace its old value with the new one. Ignored if the field name is not already set.
|
config.rename.headers
optional
Type: array of string elements
|
List of headername:value pairs. If and only if the header is already set, rename
the header. The value is unchanged. Ignored if the header is not already set.
|
config.rename.querystring
optional
Type: array of string elements
|
List of queryname:value pairs. If and only if the field name is already set, rename the field name.
The value is unchanged. Ignored if the field name is not already set.
|
config.rename.body
optional
Type: array of string elements
|
List of paramname:value pairs. Rename the parameter name if and only if
content-type is one the following [application/json , multipart/form-data , application/x-www-form-urlencoded ]
and the parameter is present.
|
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.querystring
optional
Type: array of string elements
|
List of queryname:value pairs. If and only if the querystring is not already set, set a new
querystring with the given value. Ignored if the querystring is already set.
|
config.add.body
optional
Type: array of string elements
|
List of paramname:value pairs. If and only if content-type is one the
following [application/json , multipart/form-data , application/x-www-form-urlencoded ]
and the parameter is not present, add a new parameter with the given value to the form-encoded
body. Ignored if the parameter is already present.
|
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, an additional new header with the same name and the new value will be appended.
|
config.append.querystring
optional
Type: array of string elements
|
List of queryname:value pairs. If the querystring is not set, set it with the given value. If it is already set, a new querystring with the same name and the new value will be set.
|
config.append.body
optional
Type: array of string elements
|
List of paramname:value pairs. If the content-type is one the following
[application/json , application/x-www-form-urlencoded ], add a new parameter
with the given value if the parameter is not present. Otherwise, if it is already present,
aggregate the two values (old and new) in an array.
|
Notes:
- If the value contains a
,
(comma), then the comma-separated format for lists cannot be used. The array
notation must be used instead.
- The
X-Forwarded-*
fields are non-standard header fields written by Nginx to inform the upstream about
client details and can’t be overwritten by this plugin. If you need to overwrite these header fields, see the
post-function plugin in Serverless Functions.
Template as a Value
You can use any of the current request headers, query parameters, and captured URI
groups as templates to populate supported configuration fields.
Request Parameter |
Template |
header |
$(headers.<header_name>) , $(headers["<Header-Name>"]) or $(headers["<header-name>"]) ) |
querystring |
$(query_params.<query-param-name>) or $(query_params["<query-param-name>"]) ) |
captured URIs |
$(uri_captures.<group-name>) or $(uri_captures["<group-name>"]) ) |
To escape a template, wrap it inside quotes and pass inside another template.
For example:
$('$(something_that_needs_to_escaped)')
Note: The plugin creates a non-mutable table of request headers, query strings, and captured URIs
before transformation. Therefore, any update or removal of parameters used in a template
does not affect the rendered value of a template.
Advanced templates
The content of the placeholder $(...)
is evaluated as a Lua expression, so
logical operators may be used. For example:
Header-Name:$(uri_captures["user-id"] or query_params["user"] or "unknown")
This will first look for the path parameter (uri_captures
). If not found, it will
return the query parameter. If that also doesn’t exist, it returns the default
value ‘“unknown”’.
Constant parts can be specified as part of the template outside the dynamic
placeholders. For example, creating a basic-auth header from a query parameter
called auth
that only contains the base64-encoded part:
Authorization:Basic $(query_params["auth"])
Lambdas are also supported if wrapped as an expression like this:
$((function() ... implementation here ... end)())
A complete Lambda example for prefixing a header value with “Basic” if not
already there:
Authorization:$((function()
local value = headers.Authorization
if not value then
return
end
if value:sub(1, 6) == "Basic " then
return value -- was already properly formed
end
return "Basic " .. value -- added proper prefix
end)())
Note: Especially in multi-line templates like the example above, make sure not
to add any trailing white space or new lines. Because these would be outside the
placeholders, they would be considered part of the template, and hence would be
appended to the generated value.
The environment is sandboxed, meaning that Lambdas will not have access to any
library functions, except for the string methods (like sub()
in the example
above).
Examples Using Template as A Value
Add a Service named test
which routes requests to the mockbin.com upstream service:
curl -X POST http://localhost:8001/services \
--data 'name=test' \
--data 'url=http://mockbin.com/requests'
Create a route for the test
service, capturing a user_id
field from the third segment of the request path:
Kubernetes users: Version v1beta1
of the Ingress
specification does not allow the use of named regex capture groups in paths.
If you use the ingress controller, you should use unnamed groups, e.g.
(\w+)/
instead of (?<user_id>\w+)
. You can access
these based on their order in the URL path. For example $(uri_captures[1])
obtains the value of the first capture group.
curl -X POST http://localhost:8001/services/test/routes --data "name=test_user" \
--data-urlencode 'paths=~/requests/user/(?<user_id>\w+)'
Enable the request-transformer
plugin to add a new header, x-user-id
,
whose value is being set from the captured group in the route path specified above:
curl -XPOST http://localhost:8001/routes/test_user/plugins --data "name=request-transformer" --data "config.add.headers=x-user-id:\$(uri_captures['user_id'])"
Now send a request with a user id in the route path:
curl -i -X GET localhost:8000/requests/user/foo
You should notice in the response that the x-user-id
header has been added with a value of foo
.
Order of execution
This plugin performs the response transformation in the following order:
- remove → rename → replace → add → append
Examples
Kubernetes users: Version v1beta1
of the Ingress
specification does not allow the use of named regex capture groups in paths.
If you use the ingress controller, you should use unnamed groups, e.g.
(\w+)/
instead of (?<user_id>\w+)
. You can access
these based on their order in the URL path. For example $(uri_captures[1])
obtains the value of the first capture group.
In the following examples, the plugin is enabled on a Service. This would work
similarly for Routes.
- Add multiple headers by passing each
header:value
pair separately:
With a database
Without a database
curl -X POST http://localhost:8001/services/example-service/plugins \
--data "name=request-transformer" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
plugins:
- name: request-transformer
config:
add:
headers: ["h1:v1", "h2:v1"]
incoming request headers |
upstream proxied headers: |
h1: v1 |
|
- Add multiple headers by passing comma-separated
header:value
pair (only possible with a database):
curl -X POST http://localhost:8001/services/example-service/plugins \
--data "name=request-transformer" \
--data "config.add.headers=h1:v1,h2:v2"
incoming request headers |
upstream proxied headers: |
h1: v1 |
|
- Add multiple headers passing config as a JSON body (only possible with a database):
curl -X POST http://localhost:8001/services/example-service/plugins \
--header 'content-type: application/json' \
--data '{"name": "request-transformer", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'
incoming request headers |
upstream proxied headers: |
h1: v1 |
|
- Add a querystring and a header:
With a database
Without a database
curl -X POST http://localhost:8001/services/example-service/plugins \
--data "name=request-transformer" \
--data "config.add.querystring=q1:v2,q2:v1" \
--data "config.add.headers=h1:v1"
plugins:
- name: request-transformer
config:
add:
headers: ["h1:v1"],
querystring: ["q1:v1", "q2:v2"]
incoming request headers |
upstream proxied headers: |
h1: v2 |
|
h3: v1 |
|
incoming request querystring |
upstream proxied querystring |
?q1=v1 |
?q1=v1&q2=v1 |
|
?q1=v2&q2=v1 |
- Append multiple headers and remove a body parameter:
With a database
Without a database
curl -X POST http://localhost:8001/services/example-service/plugins \
--header 'content-type: application/json' \
--data '{"name": "request-transformer", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"body": ["p1"]}}}'
plugins:
- name: request-transformer
config:
add:
headers: ["h1:v1", "h2:v1"]
remove:
body: [ "p1" ]
incoming request headers |
upstream proxied headers: |
h1: v1 |
|
incoming url encoded body |
upstream proxied url encoded body |
p1=v1&p2=v1 |
p2=v1 |
p2=v1 |
p2=v1 |