The Request Transformer plugin for IAM builds on the IAM version of this plugin with enhanced capabilities to match portions of incoming requests using regular expressions, save those matched strings into variables, and substitute those strings into transformed requests via flexible templates.
Terminology
plugin
: a plugin executing actions inside IAM before or after a request has been proxied to the upstream API.Service
: the IAM entity representing an external upstream API or microservice.Route
: the IAM entity representing a way to map downstream requests to upstream services.Consumer
: the IAM entity representing a developer or machine using the API. When using IAM, a Consumer only communicates with IAM 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 IAM, to which client requests are forwarded.API
: a legacy entity used to represent your upstream services. Deprecated in favor of Services.
Configuration
Enabling the plugin on a Service
With a database
Configure this plugin on a Service by making the following request:
$ curl -X POST http://localhost:8001/services/{service}/plugins \
--data "name=request-transformer-advanced"
Without a database
Configure this plugin on a Service by adding this section do your declarative configuration file:
plugins:
- name: request-transformer-advanced
service: {service}
config:
In both cases, {service}
is the id
or name
of the Service that this plugin configuration will target.
Enabling the plugin on a Route
With a database
Configure this plugin on a Route with:
$ curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=request-transformer-advanced"
Without a database
Configure this plugin on a Route by adding this section do your declarative configuration file:
plugins:
- name: request-transformer-advanced
route: {route}
config:
In both cases, {route}
is the id
or name
of the Route that this plugin configuration will target.
Enabling the plugin on a Consumer
With a database
You can use the http://localhost:8001/plugins
endpoint to enable this plugin
on specific Consumers:
$ curl -X POST http://localhost:8001/consumers/{consumer}/plugins \
--data "name=request-transformer-advanced" \
Without a database
Configure this plugin on a Consumer by adding this section do your declarative configuration file:
plugins:
- name: request-transformer-advanced
consumer: {consumer}
config:
In both cases, {consumer}
is the id
or username
of the Consumer that this plugin configuration will target.
You can combine consumer_id
and
service_id
in the same request, to furthermore narrow the scope of the plugin.
Global plugins
- Using a database, all plugins can be configured using the
http://localhost:8001/plugins/
endpoint. - Without a database, all plugins can be configured via the
plugins:
entry on the declarative configuration file.
A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of IAM) 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 parameter | description |
---|---|
name | The name of the plugin to use, in this case request-transformer-advanced |
service_id | The id of the Service which this plugin will target. |
route_id | The id of the Route which this plugin will target. |
enabled default value: true | Whether this plugin will be applied. |
consumer_id | The id of the Consumer which this plugin will target. |
api_id | The id of the API which this plugin will target. Note: The API Entity is deprecated in favor of Services. |
config.http_method
optional |
Changes the HTTP method for the upstream request |
config.remove.headers
optional |
List of header names. Unset the headers 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 [ |
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 querystring name is already set, replace its old value with the new one. Ignored if the header is not already set. |
config.replace.uri
optional |
Updates the upstream request URI with given value. This value can only be used to update the path part of the URI, not the scheme, nor the hostname. |
config.replace.body
optional |
List of paramname:value pairs. If and only if content-type is one the following [ |
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 [ |
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 name is not already set, set a new querystring with the given value. Ignored if the querystring name is already set. |
config.add.body
optional |
List of paramname:value pairs. If and only if content-type is one the following [ |
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.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 [ |
Note: if the value contains a ,
then the comma separated format cannot be used. The array notation must be used instead.
Template as Value
User can use any of the the current request headers, query params, and captured URI named groups as template to populate above supported config fields.
Request Param | Template |
---|---|
header | $(headers. |
querystring | $(query_params. |
captured URIs | $(uri_captures. |
To escape a template, wrap it inside quotes and pass inside another template.
Ex. $(‘$(some_needs_to_escaped)')
Note: Plugin creates a non mutable table of request headers, querystrings, and captured URIs before transformation. So any update or removal of params used in template does not affect the rendered value of template.
Examples Using Template as Value
Add an API test
with uris
configured with a named capture group user_id
$ curl -X POST http://localhost:8001/apis \
--data 'name=test' \
--data 'upstream_url=http://mockbin.com' \
--data-urlencode 'uris=/requests/user/(?<user_id>\w+)' \
--data "strip_uri=false"
Enable the ‘request-transformer-advanced' plugin to add a new header x-consumer-id
and its value is being set with the value sent with header x-user-id
or
with the default value alice is header
is missing.
$ curl -X POST http://localhost:8001/apis/test/plugins \
--data "name=request-transformer-advanced" \
--data-urlencode "config.add.headers=x-consumer-id:\$(headers['x-user-id'] or 'alice')" \
--data "config.remove.headers=x-user-id"
Now send a request without setting header x-user-id
$ curl -i -X GET localhost:8000/requests/user/foo
Plugin will add a new header x-consumer-id
with value alice before proxying
request upstream. Now try sending request with header x-user-id
set
$ curl -i -X GET localhost:8000/requests/user/foo \
-H "X-User-Id:bob"
This time plugin will add a new header x-consumer-id
with value sent along
with header x-user-id
, i.e.bob
Order of Execution
Plugin performs the response transformation in following order
remove –> replace –> add –> append
Configuration Examples
Add multiple headers by passing each header:value pair separately:
$ curl -X POST http://localhost:8001/apis/mockbin/plugins \
--data "name=request-transformer-advanced" \
--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/apis/mockbin/plugins \
--data "name=request-transformer-advanced" \
--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/apis/mockbin/plugins \
--header 'content-type: application/json' \
--data '{"name": "request-transformer-advanced", "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/apis/mockbin/plugins \
--data "name=request-transformer-advanced" \
--data "config.add.querystring=q1:v2,q2=v1" \
--data "config.add.headers=h1:v1"
Incoming Request Headers | Upstream Proxied Headers |
---|---|
h1: v1 | h1: v1, 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/apis/mockbin/plugins \
--data "name=request-transformer-advanced" \
--data "config.add.headers=h1:v2,h2:v1" \
--data "config.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 |
Add multiple headers and querystring parameters if not already set:
$ curl -X POST http://localhost:8001/apis/mockbin/plugins \
--data "name=request-transformer-advanced" \
--data "config.add.headers=h1:v1,h2:v1" \
--data "config.add.querystring=q1:v2,q2:v1" \
Incoming Request Headers | Upstream Proxied Headers |
---|---|
h1: v1 | h1: v1, 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 |