This function interrupts the current processing and produces a response.
It is typical to see plugins using it to produce a response before Kong
has a chance to proxy the request (e.g. an authentication plugin rejecting
a request, or a caching plugin serving a cached response).
It is recommended to use this function in conjunction with the return
operator, to better reflect its meaning:
return kong.response.exit(200, "Success")
Calling kong.response.exit() interrupts the execution flow of
plugins in the current phase. Subsequent phases will still be invoked.
For example, if a plugin calls kong.response.exit() in the access
phase, no other plugin is executed in that phase, but the
header_filter, body_filter, and log phases are still executed,
along with their plugins. Plugins should be programmed defensively
against cases when a request is not proxied to the Service, but
instead is produced by Kong itself. If you want to interrupt the
execution flow of plugins in the header_filter phase,
set the pdk_response_exit_header_filter_early_exit configuration to on.
-
The first argument status sets the status code of the response that
is seen by the client.
In L4 proxy mode, the status code provided is primarily for logging
and statistical purposes, and is not visible to the client directly.
In this mode, only the following status codes are supported:
- 200 - OK
- 400 - Bad request
- 403 - Forbidden
- 500 - Internal server error
- 502 - Bad gateway
- 503 - Service unavailable
-
The second, optional, body argument sets the response body. If it is
a string, no special processing is done, and the body is sent
as-is. It is the caller’s responsibility to set the appropriate
Content-Type header via the third argument.
As a convenience, body can be specified as a table. In that case,
the body is JSON-encoded and has the application/json Content-Type
header set.
On gRPC, we cannot send the body with this function, so
it sends "body" in the grpc-message header instead.
- If the body is a table, it looks for the
message field in the body,
and uses that as a grpc-message header.
- If you specify
application/grpc in the Content-Type header, the
body is sent without needing the grpc-message header.
In L4 proxy mode, body can only be nil or a string. Automatic JSON
encoding is not available. When body is provided, depending on the
value of status, the following happens:
- When
status is 500, 502 or 503, then body is logged in the Kong
error log file.
- When the
status is anything else, body is sent back to the L4 client.
-
The third, optional, headers argument can be a table specifying
response headers to send. If specified, its behavior is similar to
kong.response.set_headers(). This argument is ignored in L4 proxy mode.
Unless manually specified, this method automatically sets the
Content-Length header in the produced response for convenience.
Phases
- preread, rewrite, access, admin_api, header_filter (only if
body is nil)
Parameters
-
status (
number): The status to be used.
-
body (
table|string, optional): The body to be used.
-
headers (
table, optional): The headers to be used.
Returns
- Nothing; throws an error on invalid input.
Usage
return kong.response.exit(403, "Access Forbidden", {
["Content-Type"] = "text/plain",
["WWW-Authenticate"] = "Basic"
})
---
return kong.response.exit(403, [[{"message":"Access Forbidden"}]], {
["Content-Type"] = "application/json",
["WWW-Authenticate"] = "Basic"
})
---
return kong.response.exit(403, { message = "Access Forbidden" }, {
["WWW-Authenticate"] = "Basic"
})
---
-- In L4 proxy mode
return kong.response.exit(200, "Success")