Transform the request sent by a client on the fly on Kong, before hitting the upstream server.


Terminology

  • plugin: a plugin executing actions inside Kong before or after a request has been proxied to the upstream API.
  • Service: the Kong entity representing an external upstream API or microservice.
  • Route: the Kong entity representing a way to map downstream requests to upstream services.
  • Consumer: the Kong entity representing a developer or machine using the API. When using Kong, a Consumer only communicates with Kong which proxies every call to the said upstream API.
  • Credential: a unique string associated with a Consumer, also referred to as an API key.
  • upstream service: this refers to your own API/service sitting behind Kong, to which client requests are forwarded.
  • API: a legacy entity used to represent your upstream services. Deprecated in favor of Services since 0.13.0.

Configuration

Enabling the plugin on a Service

Configure this plugin on a Service by making the following request:

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=request-transformer"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.remove.body=formparam-toremove, formparam-another-one" \
    --data "config.rename.headers=header-old-name:header-new-name, another-old-name:another-new-name" \
    --data "config.rename.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.rename.body=param-old:param-new, param2-old:param2-new" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.querystring=new-param:some_value, another-param:some_value" \
    --data "config.add.body=new-form-param:some_value, another-form-param:some_value"
  • service: the id or name of the Service that this plugin configuration will target.

Enabling the plugin on a Route

Configure this plugin on a Route with:

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=request-transformer"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.remove.body=formparam-toremove, formparam-another-one" \
    --data "config.rename.headers=header-old-name:header-new-name, another-old-name:another-new-name" \
    --data "config.rename.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.rename.body=param-old:param-new, param2-old:param2-new" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.querystring=new-param:some_value, another-param:some_value" \
    --data "config.add.body=new-form-param:some_value, another-form-param:some_value"
  • route_id: the id of the Route that this plugin configuration will target.

Enabling the plugin on a Consumer

You can use the http://localhost:8001/plugins endpoint to enable this plugin on specific Consumers:

$ curl -X POST http://kong:8001/plugins \
    --data "name=request-transformer" \
    --data "consumer_id={consumer_id}"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.remove.body=formparam-toremove, formparam-another-one" \
    --data "config.rename.headers=header-old-name:header-new-name, another-old-name:another-new-name" \
    --data "config.rename.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.rename.body=param-old:param-new, param2-old:param2-new" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.querystring=new-param:some_value, another-param:some_value" \
    --data "config.add.body=new-form-param:some_value, another-form-param:some_value"

Where consumer_id is the id of the Consumer we want to associate with this plugin.

You can combine consumer_id and service_id

in the same request, to futhermore narrow the scope of the plugin.

Enabling the plugin on an API

If you are using an older version of Kong with the legacy API entity (deprecated since 0.13.0), you can configure this plugin on top of such an API by making the following request:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=request-transformer"  \
    --data "config.remove.headers=x-toremove, x-another-one" \
    --data "config.remove.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.remove.body=formparam-toremove, formparam-another-one" \
    --data "config.rename.headers=header-old-name:header-new-name, another-old-name:another-new-name" \
    --data "config.rename.querystring=qs-old-name:qs-new-name, qs2-old-name:qs2-new-name" \
    --data "config.rename.body=param-old:param-new, param2-old:param2-new" \
    --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value" \
    --data "config.add.headers=x-new-header:value,x-another-header:something" \
    --data "config.add.querystring=new-param:some_value, another-param:some_value" \
    --data "config.add.body=new-form-param:some_value, another-form-param:some_value"
  • api: either id or name of the API that this plugin configuration will target.

Global plugins

All plugins can be configured using the http://kong:8001/plugins/ endpoint. A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of Kong) is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.

Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

form parameterdefaultdescription
nameThe name of the plugin to use, in this case request-transformer
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
enabledtrueWhether this plugin will be applied.
consumer_idThe id of the Consumer which this plugin will target.
api_idThe id of the API which this plugin will target. Note: The API Entity is deprecated since Kong 0.13.0.
config.http_method
optional

Changes the HTTP method for the upstream request.

config.remove.headers
optional

List of header names. Unset the header(s) with the given name.

config.remove.querystring
optional

List of querystring names. Remove the querystring if it is present.

config.remove.body
optional

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 parameter 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.querystring
optional

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

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

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

List of parameter name: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 parameter is 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.replace.body
optional

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.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.querystring
optional

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 header is already set.

config.add.body
optional

List of pramname: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 form-encoded body. Ignored if the parameter 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, an additional new header with the same name and the new value will be appended.

config.append.querystring
optional

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

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, 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.

Dynamic Transformation Based on Request Content

The Request Transformer plugin bundled with Mashape Enterprise Edition allows for adding or replacing content in the upstream request based on variable data found in the client request, such as request headers, query string parameters, or URI parameters as defined by a URI capture group.

If you already are a Mashape Enterprise customer, you can request access to this plugin functionality by opening a support ticket using your Enterprise support channels.

If you are not a Mashape Enterprise customer, you can inquire about our Enterprise offering by contacting us.

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 Service. This would work similarly for Routes, or the depreciated API entity.

  • Add multiple headers by passing each header:value pair separately:
$ 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"
incoming request headers upstream proxied headers:
h1: v1
  • h1: v1
  • h2: v1

  • Add multiple headers by passing comma separated header:value pair:
$ 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
  • h1: v1
  • h2: v1

  • Add multiple headers passing config as JSON body:
$ 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
  • h1: v1
  • h2: v1

  • Add a querystring and a header:
$ 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"
incoming request headers upstream proxied headers:
h1: v2
  • h1: v2
  • h2: v1
h3: v1
  • h1: v1
  • h2: v1
  • 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:
$ 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"]}}}'
incoming request headers upstream proxied headers:
h1: v1
  • h1: v1
  • h1: v2
  • h2: v1
incoming url encoded body upstream proxied url encoded body:
p1=v1&p2=v1 p2=v1
p2=v1 p2=v1

Keep up with the latest features