Community Plugin: This plugin is developed, tested, and maintained by a third-party contributor.
This is a Kong Plugin that transforms requests and responses depending on your own business requirements.
For questions, details or contributions, please reach us via the API Transformer GitHub repo.
local _req_uri = ngx.var.uri
local _req_method = ngx.req.get_method()
local _req_json_body = ngx.ctx.req_json_body
if _req_uri == string.match(_req_uri, ".-/folders") then
-- modified body will pass to upstream
if _req_method == "GET" then
return true, '{"key": "GET /folders: body modified"}'
elseif _req_method == "POST" then
return true, '{"key": "POST /folders: body modified"}'
end
else
return false, "invalid request uri: " .. _req_uri
end
local _req_uri = ngx.ctx.req_uri
local _req_method = ngx.ctx.req_method
local _req_json_body = ngx.ctx.req_json_body
local _resp_json_body = ngx.ctx.resp_json_body
if _req_uri == string.match(_req_uri, ".-/folders") then
-- modified body will return to client
if _req_method == "GET" then
return true, '{"key": "GET /folders: body modified"}'
elseif _req_method == "POST" then
return true, '{"key": "POST /folders: body modified"}'
end
else
return false, "invalid request uri: " .. _req_uri
end
print
assert
error
ipairs
next
pairs
pcall
select
tonumber
tostring
type
unpack
xpcall
string.byte
string.char
string.find
string.format
string.gmatch
string.gsub
string.len
string.match
string.rep
string.reverse
string.sub
string.upper
table.insert
table.maxn
table.remove
table.sort
table.insert
table.concate
ngx.ctx
ngx.var
ngx.req.get_headers
ngx.req.set_header
ngx.req.get_method
ngx.req.get_body_data
ngx.req.set_body_data
ngx.req.get_uri_args
ngx.req.set_uri_args
ngx.resp.get_headers
This table ngx.ctx
can be used to store per-request Lua context data and has a life time identical to the current request, so we use this table to store the necessary data for body_filter()
Cached Symbols |
Coreresponding |
Lua type |
ngx.ctx.req_uri |
ngx.var.uri |
string |
ngx.ctx.req_method |
ngx.req.get_method() |
string |
ngx.ctx.req_json_body |
_cjson_decode_(ngx.req.get_body_data()) |
table |
ngx.ctx.resp_json_body |
ngx.arg[1] |
talbe |
In the transformer, we need to return a Lua tuple: (f_status, body_or_err)
, please check the detail via test case.
if f_status == true then
body_or_err = transformed_body
else
body_or_err = error message
end
Configuration Reference
This plugin is compatible with DB-less mode.
Example plugin configuration
Enable on a service
Enable on a route
Enable globally
The following examples provide some typical configurations for enabling
the api-transformer
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=api-transformer" \
--data "config.request_transformer=/home/foo/api_xxx/req_transformer.lua" \
--data "config.response_transformer=/home/foo/api_xxx/resp_transformer.lua" \
--data "config.http_200_always=true"
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: api-transformer-example
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
plugin: api-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: api-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: api-transformer
service: SERVICE_NAME|SERVICE_ID
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
The following examples provide some typical configurations for enabling
the api-transformer
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=api-transformer" \
--data "config.request_transformer=/home/foo/api_xxx/req_transformer.lua" \
--data "config.response_transformer=/home/foo/api_xxx/resp_transformer.lua" \
--data "config.http_200_always=true"
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: api-transformer-example
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
plugin: api-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: api-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: api-transformer
route: ROUTE_NAME
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
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 api-transformer
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=api-transformer" \
--data "config.request_transformer=/home/foo/api_xxx/req_transformer.lua" \
--data "config.response_transformer=/home/foo/api_xxx/resp_transformer.lua" \
--data "config.http_200_always=true"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-api-transformer>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
plugin: api-transformer
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: api-transformer
config:
request_transformer: /home/foo/api_xxx/req_transformer.lua
response_transformer: /home/foo/api_xxx/resp_transformer.lua
http_200_always: true
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 api-transformer . |
service.name or service.id
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
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 . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.request_transformer
|
The .lua script to be used for the transformation.
Available OpenResty variables and utils: Check README
|
config.response_transformer
|
The .lua script to be used for the transformation.
Available OpenResty variables and utils: Check README
|
config.http_200_always
Default value: true
|
We may need to use HTTP 200 approach in API error handling in some business cases.
|