All custom
fields support Lua expressions in the value portion, and any PDK method
or Lua function available within the Kong Gateway sandbox can be used. The syntax is the
same as the Request Transformer Advanced plugin
uses for Lua expressions.
In custom
values, callouts can be referenced via the shorthand callouts.CALLOUT_NAME
instead of the full kong.ngx.shared.callouts.CALLOUT_NAME
.
Lua expressions don’t carry side effects.
by_lua
fields work in a similar way, but they don’t support shortcuts.
Shortcuts can produce unintended side effects and modify callout and upstream requests.
Both request and response callout objects may contain a by_lua
field:
-
request.by_lua
runs before the callout request is performed and is useful to
further customize aspects of the request.
-
response.by_lua
runs after a response is obtained, and is useful to
customize aspects of the response such as caching.
The upstream object may also contain a by_lua
field for Lua code
that runs before the upstream request runs. This is useful to further customize
the upstream request, or even to bypass it completely, short-circuiting the
request and response from Kong Gateway.
Lua code may contain references and modify values in the callout context.
Schema validation will detect syntax issues. Other errors, such as
nil references, happen at runtime and will lead to an Internal Server Error
.
Lua code must be thoroughly tested to ensure that it’s correct and meets
performance requirements.