Community Plugin: This plugin is developed, tested, and maintained by a third-party contributor.
Transform the request sent by a client on the fly on Kong, before hitting the upstream server.
The plugin implements parameter transformations and additions of various positions.
Installation
Recommended:
$ git clone https://github.com/cheriL/apig-request-transformer /opt/kong/plugins
$ cd /opt/kong/plugins/apig-request-transformer
$ luarocks make
Configuration Reference
This plugin is not compatible with DB-less mode.
It is recommended to use in dbless mode.
Enable the plugin on a service
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin on a service by
making the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/plugins \
--data "name=apig-request-transformer" \
--data "config.httpMethod=POST" \
--data "config.backendContentType=application/json" \
--data "config.requestPath=/requestPath/[pageId]/[userId]" \
--data "config.backendPath=/servicePath/[pageId]/[userId]" \
--data "config.pathParams=pageId" \
--data "config.pathParams=userId" \
--data "config.replace=head:h1;query:param1" \
--data "config.replace=path:pageId;query:param2" \
--data "config.add=head:h1:v1" \
--data "config.add=query:param1:value1"
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: <apig-request-transformer-example>
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
plugin: apig-request-transformer
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: <apig-request-transformer-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
.
For example, configure this plugin on a service by
adding this section to your declarative configuration file:
plugins:
- name: apig-request-transformer
service: {SERVICE}
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
Enable the plugin on a route
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin on a route with:
$ curl -X POST http://{HOST}:8001/routes/{ROUTE}/plugins \
--data "name=apig-request-transformer" \
--data "config.httpMethod=POST" \
--data "config.backendContentType=application/json" \
--data "config.requestPath=/requestPath/[pageId]/[userId]" \
--data "config.backendPath=/servicePath/[pageId]/[userId]" \
--data "config.pathParams=pageId" \
--data "config.pathParams=userId" \
--data "config.replace=head:h1;query:param1" \
--data "config.replace=path:pageId;query:param2" \
--data "config.add=head:h1:v1" \
--data "config.add=query:param1:value1"
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: <apig-request-transformer-example>
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
plugin: apig-request-transformer
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: <apig-request-transformer-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
.
For example, configure this plugin on a route by
adding this section to your declarative configuration file:
plugins:
- name: apig-request-transformer
route: <route>
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Enable the plugin globally
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.
Admin API
Kubernetes
Declarative (YAML)
For example, configure this plugin globally with:
$ curl -X POST http://{HOST}:8001/plugins/ \
--data "name=apig-request-transformer" \
--data "config.httpMethod=POST" \
--data "config.backendContentType=application/json" \
--data "config.requestPath=/requestPath/[pageId]/[userId]" \
--data "config.backendPath=/servicePath/[pageId]/[userId]" \
--data "config.pathParams=pageId" \
--data "config.pathParams=userId" \
--data "config.replace=head:h1;query:param1" \
--data "config.replace=path:pageId;query:param2" \
--data "config.add=head:h1:v1" \
--data "config.add=query:param1:value1"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-apig-request-transformer>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
plugin: apig-request-transformer
For example, configure this plugin using the plugins:
entry in the declarative
configuration file:
plugins:
- name: apig-request-transformer
config:
httpMethod: POST
backendContentType: application/json
requestPath: /requestPath/[pageId]/[userId]
backendPath: /servicePath/[pageId]/[userId]
pathParams:
- pageId
- userId
replace:
- head:h1;query:param1
- path:pageId;query:param2
add:
- head:h1:v1
- query:param1:value1
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 apig-request-transformer . |
service.id
Type: string |
The ID of the Service the plugin targets. |
route.id
Type: string |
The ID of the Route the plugin targets. |
enabled
required
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.httpMethod
required
|
Changes the HTTP method for the upstream request.
|
config.backendContentType
optional
|
Changes Content-Type for the upstream request.
|
config.requestPath
optional
|
It is used with [config.pathParams] to extract values from the request URI.
|
config.backendPath
optional
|
Updates the the path part of the upstream request URI with values.
|
config.pathParams
semi-optional
|
List of parameters’ names of the request URI.
|
config.replace
optional
|
List of parameter mappings. Replace its old value with the new one at different locations.
|
config.add
optional
|
List of constant parameters. Set a new header or a new querystring with the given value.
|