kong.service package

Submodules

kong.service.request module

class kong.service.request.request[source]

Bases: object

static add_header(header: str, of: table) None[source]

Adds a request header with the given value to the request to the Service. Unlike kong.service.request.set_header(), this function doesn’t remove any existing headers with the same name. Instead, several occurrences of the header will be present in the request. The order in which headers are added is retained.

Phases:

rewrite, access

Example:

kong.service.request.add_header(“Cache-Control”, “no-cache”)

kong.service.request.add_header(“Cache-Control”, “no-store”)

Parameters:
  • header (str) – The header name. Example: “Cache-Control”.

  • of (table) – strings|string|number|boolean value The header value. Example: “no-cache”.

Returns:

throws an error on invalid inputs.

Return type:

None

static clear_header(header: str) None[source]

Removes all occurrences of the specified header from the request to the Service.

Phases:

rewrite, access

Example:

kong.service.request.set_header(“X-Foo”, “foo”)

kong.service.request.add_header(“X-Foo”, “bar”)

kong.service.request.clear_header(“X-Foo”)

# from here onwards, no X-Foo headers will exist in the request

Parameters:

header (str) – The header name. Example: “X-Foo”.

Returns:

throws an error on invalid inputs. The function does not throw an error if no header was removed.

Return type:

None

static disable_tls() Tuple[bool, str][source]

Disables the TLS handshake to upstream for [ngx_stream_proxy_module](https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html). This overrides the [proxy_ssl](https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_ssl) directive, effectively setting it to off for the current stream session. Once this function has been called, it is not possible to re-enable TLS handshake for the current session.

Phases:

preread, balancer

Example:

ok, err = kong.service.request.disable_tls()

if not ok:

# do something with error

Returns:

true if the operation succeeded, nil if an error occurred.

Return type:

bool

Returns:

An error message describing the error if there was one.

Return type:

str

static enable_buffering() None[source]

Enables buffered proxying, which allows plugins to access Service body and response headers at the same time.

Phases:

rewrite, access, balancer

Example:

kong.service.request.enable_buffering()

Returns:

Return type:

None

static set_body(args: table, mimetype: str | None) Tuple[bool, str][source]

Sets the body of the request to the Service. Unlike kong.service.request.set_raw_body(), the args argument must be a table, and is encoded with a MIME type. The encoding MIME type can be specified in the optional mimetype argument, or if left unspecified, is chosen based on the Content-Type header of the client’s request. Behavior based on MIME type in the Content-Type header: * application/x-www-form-urlencoded: Encodes the arguments as

form-encoded. Keys are produced in lexicographical order. The order of entries within the same key (when values are given as an array) is retained. Any string values given are URL-encoded.

  • multipart/form-data: Encodes the arguments as multipart form data.

  • application/json: Encodes the arguments as JSON (same as kong.service.request.set_raw_body(json.encode(args))). Lua types are converted to matching JSON types.

If the MIME type is none of the above, this function returns nil and an error message indicating the body could not be encoded. If the mimetype argument is specified, the Content-Type header is set accordingly in the request to the Service. If further control of the body generation is needed, a raw body can be given as a string with kong.service.request.set_raw_body().

Phases:

rewrite, access, balancer

Example:

kong.service.set_header(“application/json”)

ok, err = kong.service.request.set_body({

name = “John Doe”,

age = 42,

numbers = {1, 2, 3}

})

# Produces the following JSON body:

# { “name”: “John Doe”, “age”: 42, “numbers”:[1, 2, 3] }

ok, err = kong.service.request.set_body({

foo = “hello world”,

bar = {“baz”, “bla”, true},

zzz = true,

blo = “”

}, “application/x-www-form-urlencoded”)

# Produces the following body:

# bar=baz&bar=bla&bar&blo=&foo=hello%20world&zzz

Parameters:
  • args (table) – A table with data to be converted to the appropriate format and stored in the body.

  • mimetype (str) – can be one of:

Returns:

true on success, nil otherwise.

Return type:

bool

Returns:

nil on success, an error message in case of error. Throws an error on invalid inputs.

Return type:

str

static set_header(header: str, of: table) None[source]

Sets a header in the request to the Service with the given value. Any existing header with the same name will be overridden. If the header argument is “host” (case-insensitive), then this also sets the SNI of the request to the Service.

Phases:

rewrite, access, balancer

Example:

kong.service.request.set_header(“X-Foo”, “value”)

Parameters:
  • header (str) – The header name. Example: “X-Foo”.

  • of (table) – strings|string|boolean|number value The header value. Example: “hello world”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_headers(headers: table) None[source]

Sets the headers of the request to the Service. Unlike kong.service.request.set_header(), the headers argument must be a table in which each key is a string (corresponding to a header’s name), and each value is a string, or an array of strings. The resulting headers are produced in lexicographical order. The order of entries with the same name (when values are given as an array) is retained. This function overrides any existing header bearing the same name as those specified in the headers argument. Other headers remain unchanged. If the “Host” header is set (case-insensitive), then this also sets the SNI of the request to the Service.

Phases:

rewrite, access

Example:

kong.service.request.set_header(“X-Foo”, “foo1”)

kong.service.request.add_header(“X-Foo”, “foo2”)

kong.service.request.set_header(“X-Bar”, “bar1”)

kong.service.request.set_headers({

[“X-Foo”] = “foo3”,

[“Cache-Control”] = { “no-store”, “no-cache” },

[“Bla”] = “boo”

})

# Will add the following headers to the request, in this order:

# X-Bar: bar1

# Bla: boo

# Cache-Control: no-store

# Cache-Control: no-cache

# X-Foo: foo3

Parameters:

headers (table) – A table where each key is a string containing a header name and each value is either a string or an array of strings.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_method(method: str) None[source]

Sets the HTTP method for the request to the service.

Phases:

rewrite, access

Example:

kong.service.request.set_method(“DELETE”)

Parameters:

method (str) – The method string, which must be in all uppercase. Supported values are: “GET”, “HEAD”, “PUT”, “POST”, “DELETE”, “OPTIONS”, “MKCOL”, “COPY”, “MOVE”, “PROPFIND”, “PROPPATCH”, “LOCK”, “UNLOCK”, “PATCH”, or “TRACE”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_path(path: str) None[source]

Sets the path component for the request to the service. The input accepts any valid normalized URI (including UTF-8 characters) and this API will perform necessary escaping according to the RFC to make the request valid. Input should not include the query string.

Phases:

access, rewrite, balancer

Example:

kong.service.request.set_path(“/v2/movies”)

Parameters:

path (str) – The path string. Special characters and UTF-8 characters are allowed, for example: “/v2/movies” or “/foo/😀”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_query(args: table) None[source]

Set the query string of the request to the Service. Unlike kong.service.request.set_raw_query(), the query argument must be a table in which each key is a string (corresponding to an argument’s name), and each value is either a boolean, a string, or an array of strings or booleans. Additionally, all string values will be URL-encoded. The resulting query string contains keys in their lexicographical order. The order of entries within the same key (when values are given as an array) is retained. If further control of the query string generation is needed, a raw query string can be given as a string with kong.service.request.set_raw_query().

Phases:

rewrite, access

Example:

kong.service.request.set_query({

foo = “hello world”,

bar = {“baz”, “bla”, true},

zzz = true,

blo = “”

})

# Produces the following query string:

# bar=baz&bar=bla&bar&blo=&foo=hello%20world&zzz

Parameters:

args (table) – A table where each key is a string (corresponding to an argument name), and each value is either a boolean, a string, or an array of strings or booleans. Any string values given are URL-encoded.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_raw_body(body: str) None[source]

Sets the body of the request to the Service. The body argument must be a string and will not be processed in any way. This function also sets the Content-Length header appropriately. To set an empty body, you can provide an empty string (“”) to this function. For a higher-level function to set the body based on the request content type, see kong.service.request.set_body().

Phases:

rewrite, access, balancer

Example:

kong.service.request.set_raw_body(“Hello, world!”)

Parameters:

body (str) – The raw body.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_raw_query(query: str) None[source]

Sets the query string of the request to the Service. The query argument is a string (without the leading ? character), and is not processed in any way. For a higher-level function to set the query string from a Lua table of arguments, see kong.service.request.set_query().

Phases:

rewrite, access

Example:

kong.service.request.set_raw_query(“zzz&bar=baz&bar=bla&bar&blo=&foo=hello%20world”)

Parameters:

query (str) – The raw querystring. Example: “foo=bar&bla&baz=hello%20world”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_scheme(scheme: str) None[source]

Sets the protocol to use when proxying the request to the Service.

Phases:

access, rewrite, balancer

Example:

kong.service.request.set_scheme(“https”)

Parameters:

scheme (str) – The scheme to be used. Supported values are “http” or “https”.

Returns:

throws an error on invalid inputs.

Return type:

None

kong.service.response module

class kong.service.response.response[source]

Bases: object

static get_body(mimetype: str | None, max_args: number | None) str[source]

Returns the decoded buffered body.

Phases:

header_filter, body_filter, log

Example:

# Plugin needs to call kong.service.request.enable_buffering() on rewrite

# or access phase prior calling this function.

body = kong.service.response.get_body()

Parameters:
  • mimetype (str) – The MIME type of the response (if known).

  • max_args (number) – Sets a limit on the maximum number of (what?) that can be parsed.

Returns:

The decoded buffered body

Return type:

str

static get_header(name: str) str[source]

Returns the value of the specified response header. Unlike kong.response.get_header(), this function only returns a header if it is present in the response from the Service (ignoring headers added by Kong itself).

Phases:

header_filter, body_filter, log

Example:

# Given a response with the following headers:

# X-Custom-Header: bla

# X-Another: foo bar

# X-Another: baz

kong.log.inspect(kong.service.response.get_header(“x-custom-header”)) # “bla”

kong.log.inspect(kong.service.response.get_header(“X-Another”)) # “foo bar”

Parameters:

name (str) – The name of the header. Header names in are case-insensitive and are normalized to lowercase, and dashes (-) can be written as underscores (_); that is, the header X-Custom-Header can also be retrieved as x_custom_header.

Returns:

The value of the header, or nil if a header with name is not found in the response. If a header with the same name is present multiple times in the response, this function returns the value of the first occurrence of this header.

Return type:

str

static get_headers(max_headers: number | None) Tuple[table, str][source]

Returns a Lua table holding the headers from the Service response. Keys are header names. Values are either a string with the header value, or an array of strings if a header was sent multiple times. Header names in this table are case-insensitive and dashes (-) can be written as underscores (_); that is, the header X-Custom-Header can also be retrieved as x_custom_header. Unlike kong.response.get_headers(), this function only returns headers that are present in the response from the Service (ignoring headers added by Kong itself). If the request is not proxied to a Service (e.g. an authentication plugin rejected a request and produced an HTTP 401 response), then the returned headers value might be nil, since no response from the Service has been received. By default, this function returns up to 100 headers. The optional max_headers argument can be specified to customize this limit, but must be greater than 1 and not greater than 1000.

Phases:

header_filter, body_filter, log

Example:

# Given a response with the following headers:

# X-Custom-Header: bla

# X-Another: foo bar

# X-Another: baz

headers = kong.service.response.get_headers()

if headers:

kong.log.inspect(headers.x_custom_header) # “bla”

kong.log.inspect(headers.x_another[1]) # “foo bar”

kong.log.inspect(headers[“X-Another”][2]) # “baz”Note that this function returns a proxy table

which cannot be iterated with pairs or used as operand of #.

Parameters:

max_headers (number) – Sets a limit on the maximum number of headers that can be parsed.

Returns:

The response headers in table form.

Return type:

table

Returns:

If more headers than max_headers are present, returns a string with the error “truncated”.

Return type:

str

static get_raw_body() bytes[source]

Returns the raw buffered body.

Phases:

header_filter, body_filter, log

Example:

# Plugin needs to call kong.service.request.enable_buffering() on rewrite

# or access phase prior calling this function.

body = kong.service.response.get_raw_body()

Returns:

The raw buffered body.

Return type:

bytes

static get_status() number[source]

Returns the HTTP status code of the response from the Service as a Lua number.

Phases:

header_filter, body_filter, log

Example:

kong.log.inspect(kong.service.response.get_status()) # 418

Returns:

The status code from the response from the Service, or nil if the request was not proxied (that is, if kong.response.get_source() returned anything other than “service”).

Return type:

number

Module contents

class kong.service.service[source]

Bases: object

class request

Bases: object

static add_header(header: str, of: table) None

Adds a request header with the given value to the request to the Service. Unlike kong.service.request.set_header(), this function doesn’t remove any existing headers with the same name. Instead, several occurrences of the header will be present in the request. The order in which headers are added is retained.

Phases:

rewrite, access

Example:

kong.service.request.add_header(“Cache-Control”, “no-cache”)

kong.service.request.add_header(“Cache-Control”, “no-store”)

Parameters:
  • header (str) – The header name. Example: “Cache-Control”.

  • of (table) – strings|string|number|boolean value The header value. Example: “no-cache”.

Returns:

throws an error on invalid inputs.

Return type:

None

static clear_header(header: str) None

Removes all occurrences of the specified header from the request to the Service.

Phases:

rewrite, access

Example:

kong.service.request.set_header(“X-Foo”, “foo”)

kong.service.request.add_header(“X-Foo”, “bar”)

kong.service.request.clear_header(“X-Foo”)

# from here onwards, no X-Foo headers will exist in the request

Parameters:

header (str) – The header name. Example: “X-Foo”.

Returns:

throws an error on invalid inputs. The function does not throw an error if no header was removed.

Return type:

None

static disable_tls() Tuple[bool, str]

Disables the TLS handshake to upstream for [ngx_stream_proxy_module](https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html). This overrides the [proxy_ssl](https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_ssl) directive, effectively setting it to off for the current stream session. Once this function has been called, it is not possible to re-enable TLS handshake for the current session.

Phases:

preread, balancer

Example:

ok, err = kong.service.request.disable_tls()

if not ok:

# do something with error

Returns:

true if the operation succeeded, nil if an error occurred.

Return type:

bool

Returns:

An error message describing the error if there was one.

Return type:

str

static enable_buffering() None

Enables buffered proxying, which allows plugins to access Service body and response headers at the same time.

Phases:

rewrite, access, balancer

Example:

kong.service.request.enable_buffering()

Returns:

Return type:

None

static set_body(args: table, mimetype: str | None) Tuple[bool, str]

Sets the body of the request to the Service. Unlike kong.service.request.set_raw_body(), the args argument must be a table, and is encoded with a MIME type. The encoding MIME type can be specified in the optional mimetype argument, or if left unspecified, is chosen based on the Content-Type header of the client’s request. Behavior based on MIME type in the Content-Type header: * application/x-www-form-urlencoded: Encodes the arguments as

form-encoded. Keys are produced in lexicographical order. The order of entries within the same key (when values are given as an array) is retained. Any string values given are URL-encoded.

  • multipart/form-data: Encodes the arguments as multipart form data.

  • application/json: Encodes the arguments as JSON (same as kong.service.request.set_raw_body(json.encode(args))). Lua types are converted to matching JSON types.

If the MIME type is none of the above, this function returns nil and an error message indicating the body could not be encoded. If the mimetype argument is specified, the Content-Type header is set accordingly in the request to the Service. If further control of the body generation is needed, a raw body can be given as a string with kong.service.request.set_raw_body().

Phases:

rewrite, access, balancer

Example:

kong.service.set_header(“application/json”)

ok, err = kong.service.request.set_body({

name = “John Doe”,

age = 42,

numbers = {1, 2, 3}

})

# Produces the following JSON body:

# { “name”: “John Doe”, “age”: 42, “numbers”:[1, 2, 3] }

ok, err = kong.service.request.set_body({

foo = “hello world”,

bar = {“baz”, “bla”, true},

zzz = true,

blo = “”

}, “application/x-www-form-urlencoded”)

# Produces the following body:

# bar=baz&bar=bla&bar&blo=&foo=hello%20world&zzz

Parameters:
  • args (table) – A table with data to be converted to the appropriate format and stored in the body.

  • mimetype (str) – can be one of:

Returns:

true on success, nil otherwise.

Return type:

bool

Returns:

nil on success, an error message in case of error. Throws an error on invalid inputs.

Return type:

str

static set_header(header: str, of: table) None

Sets a header in the request to the Service with the given value. Any existing header with the same name will be overridden. If the header argument is “host” (case-insensitive), then this also sets the SNI of the request to the Service.

Phases:

rewrite, access, balancer

Example:

kong.service.request.set_header(“X-Foo”, “value”)

Parameters:
  • header (str) – The header name. Example: “X-Foo”.

  • of (table) – strings|string|boolean|number value The header value. Example: “hello world”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_headers(headers: table) None

Sets the headers of the request to the Service. Unlike kong.service.request.set_header(), the headers argument must be a table in which each key is a string (corresponding to a header’s name), and each value is a string, or an array of strings. The resulting headers are produced in lexicographical order. The order of entries with the same name (when values are given as an array) is retained. This function overrides any existing header bearing the same name as those specified in the headers argument. Other headers remain unchanged. If the “Host” header is set (case-insensitive), then this also sets the SNI of the request to the Service.

Phases:

rewrite, access

Example:

kong.service.request.set_header(“X-Foo”, “foo1”)

kong.service.request.add_header(“X-Foo”, “foo2”)

kong.service.request.set_header(“X-Bar”, “bar1”)

kong.service.request.set_headers({

[“X-Foo”] = “foo3”,

[“Cache-Control”] = { “no-store”, “no-cache” },

[“Bla”] = “boo”

})

# Will add the following headers to the request, in this order:

# X-Bar: bar1

# Bla: boo

# Cache-Control: no-store

# Cache-Control: no-cache

# X-Foo: foo3

Parameters:

headers (table) – A table where each key is a string containing a header name and each value is either a string or an array of strings.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_method(method: str) None

Sets the HTTP method for the request to the service.

Phases:

rewrite, access

Example:

kong.service.request.set_method(“DELETE”)

Parameters:

method (str) – The method string, which must be in all uppercase. Supported values are: “GET”, “HEAD”, “PUT”, “POST”, “DELETE”, “OPTIONS”, “MKCOL”, “COPY”, “MOVE”, “PROPFIND”, “PROPPATCH”, “LOCK”, “UNLOCK”, “PATCH”, or “TRACE”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_path(path: str) None

Sets the path component for the request to the service. The input accepts any valid normalized URI (including UTF-8 characters) and this API will perform necessary escaping according to the RFC to make the request valid. Input should not include the query string.

Phases:

access, rewrite, balancer

Example:

kong.service.request.set_path(“/v2/movies”)

Parameters:

path (str) – The path string. Special characters and UTF-8 characters are allowed, for example: “/v2/movies” or “/foo/😀”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_query(args: table) None

Set the query string of the request to the Service. Unlike kong.service.request.set_raw_query(), the query argument must be a table in which each key is a string (corresponding to an argument’s name), and each value is either a boolean, a string, or an array of strings or booleans. Additionally, all string values will be URL-encoded. The resulting query string contains keys in their lexicographical order. The order of entries within the same key (when values are given as an array) is retained. If further control of the query string generation is needed, a raw query string can be given as a string with kong.service.request.set_raw_query().

Phases:

rewrite, access

Example:

kong.service.request.set_query({

foo = “hello world”,

bar = {“baz”, “bla”, true},

zzz = true,

blo = “”

})

# Produces the following query string:

# bar=baz&bar=bla&bar&blo=&foo=hello%20world&zzz

Parameters:

args (table) – A table where each key is a string (corresponding to an argument name), and each value is either a boolean, a string, or an array of strings or booleans. Any string values given are URL-encoded.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_raw_body(body: str) None

Sets the body of the request to the Service. The body argument must be a string and will not be processed in any way. This function also sets the Content-Length header appropriately. To set an empty body, you can provide an empty string (“”) to this function. For a higher-level function to set the body based on the request content type, see kong.service.request.set_body().

Phases:

rewrite, access, balancer

Example:

kong.service.request.set_raw_body(“Hello, world!”)

Parameters:

body (str) – The raw body.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_raw_query(query: str) None

Sets the query string of the request to the Service. The query argument is a string (without the leading ? character), and is not processed in any way. For a higher-level function to set the query string from a Lua table of arguments, see kong.service.request.set_query().

Phases:

rewrite, access

Example:

kong.service.request.set_raw_query(“zzz&bar=baz&bar=bla&bar&blo=&foo=hello%20world”)

Parameters:

query (str) – The raw querystring. Example: “foo=bar&bla&baz=hello%20world”.

Returns:

throws an error on invalid inputs.

Return type:

None

static set_scheme(scheme: str) None

Sets the protocol to use when proxying the request to the Service.

Phases:

access, rewrite, balancer

Example:

kong.service.request.set_scheme(“https”)

Parameters:

scheme (str) – The scheme to be used. Supported values are “http” or “https”.

Returns:

throws an error on invalid inputs.

Return type:

None

class response

Bases: object

static get_body(mimetype: str | None, max_args: number | None) str

Returns the decoded buffered body.

Phases:

header_filter, body_filter, log

Example:

# Plugin needs to call kong.service.request.enable_buffering() on rewrite

# or access phase prior calling this function.

body = kong.service.response.get_body()

Parameters:
  • mimetype (str) – The MIME type of the response (if known).

  • max_args (number) – Sets a limit on the maximum number of (what?) that can be parsed.

Returns:

The decoded buffered body

Return type:

str

static get_header(name: str) str

Returns the value of the specified response header. Unlike kong.response.get_header(), this function only returns a header if it is present in the response from the Service (ignoring headers added by Kong itself).

Phases:

header_filter, body_filter, log

Example:

# Given a response with the following headers:

# X-Custom-Header: bla

# X-Another: foo bar

# X-Another: baz

kong.log.inspect(kong.service.response.get_header(“x-custom-header”)) # “bla”

kong.log.inspect(kong.service.response.get_header(“X-Another”)) # “foo bar”

Parameters:

name (str) – The name of the header. Header names in are case-insensitive and are normalized to lowercase, and dashes (-) can be written as underscores (_); that is, the header X-Custom-Header can also be retrieved as x_custom_header.

Returns:

The value of the header, or nil if a header with name is not found in the response. If a header with the same name is present multiple times in the response, this function returns the value of the first occurrence of this header.

Return type:

str

static get_headers(max_headers: number | None) Tuple[table, str]

Returns a Lua table holding the headers from the Service response. Keys are header names. Values are either a string with the header value, or an array of strings if a header was sent multiple times. Header names in this table are case-insensitive and dashes (-) can be written as underscores (_); that is, the header X-Custom-Header can also be retrieved as x_custom_header. Unlike kong.response.get_headers(), this function only returns headers that are present in the response from the Service (ignoring headers added by Kong itself). If the request is not proxied to a Service (e.g. an authentication plugin rejected a request and produced an HTTP 401 response), then the returned headers value might be nil, since no response from the Service has been received. By default, this function returns up to 100 headers. The optional max_headers argument can be specified to customize this limit, but must be greater than 1 and not greater than 1000.

Phases:

header_filter, body_filter, log

Example:

# Given a response with the following headers:

# X-Custom-Header: bla

# X-Another: foo bar

# X-Another: baz

headers = kong.service.response.get_headers()

if headers:

kong.log.inspect(headers.x_custom_header) # “bla”

kong.log.inspect(headers.x_another[1]) # “foo bar”

kong.log.inspect(headers[“X-Another”][2]) # “baz”Note that this function returns a proxy table

which cannot be iterated with pairs or used as operand of #.

Parameters:

max_headers (number) – Sets a limit on the maximum number of headers that can be parsed.

Returns:

The response headers in table form.

Return type:

table

Returns:

If more headers than max_headers are present, returns a string with the error “truncated”.

Return type:

str

static get_raw_body() bytes

Returns the raw buffered body.

Phases:

header_filter, body_filter, log

Example:

# Plugin needs to call kong.service.request.enable_buffering() on rewrite

# or access phase prior calling this function.

body = kong.service.response.get_raw_body()

Returns:

The raw buffered body.

Return type:

bytes

static get_status() number

Returns the HTTP status code of the response from the Service as a Lua number.

Phases:

header_filter, body_filter, log

Example:

kong.log.inspect(kong.service.response.get_status()) # 418

Returns:

The status code from the response from the Service, or nil if the request was not proxied (that is, if kong.response.get_source() returned anything other than “service”).

Return type:

number

static set_retries(retries: number) None[source]

Sets the retries count for the current request. This will override the default retries count set in the Upstream entity. The retries argument expects an integer between 0 and 32767.

Phases:

access

Example:

kong.service.set_retries(233)

Parameters:

retries (number)

static set_target(host: str, port: number) None[source]

Sets the host and port on which to connect to for proxying the request. Using this method is equivalent to ask Kong to not run the load-balancing phase for this request, and consider it manually overridden. Load-balancing components such as retries and health-checks will also be ignored for this request. Use kong.service.set_retries to overwrite retries count. The host argument expects the hostname or IP address of the upstream server, and the port expects a port number.

Phases:

access

Example:

kong.service.set_target(“service.local”, 443)

kong.service.set_target(“192.168.130.1”, 80)

Parameters:
  • host (str)

  • port (number)

static set_timeouts(connect_timeout: number, write_timeout: number, read_timeout: number) None[source]

Sets the timeouts for the current request. This will override the default timeouts set in the Upstream entity.

The connect_timeout, write_timeout, and read_timeout arguments expect an integer between 1 and 2147483646.

Phases:

access

Example:

kong.service.set_timeouts(233, 233, 233)

Parameters:
  • connect_timeout (number)

  • write_timeout (number)

  • read_timeout (number)

static set_tls_verify(on: bool) Tuple[bool, str][source]

Sets whether TLS verification is enabled while handshaking with the Service. The on argument is a boolean flag, where true means upstream verification is enabled and false disables it. This call affects only the current request. If the trusted certificate store is not set already (via [proxy_ssl_trusted_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_trusted_certificate) or [kong.service.set_upstream_ssl_trusted_store](#kongserviceset_upstream_ssl_trusted_store)), then TLS verification will always fail with “unable to get local issuer certificate” error.

Phases:

rewrite, access, balancer, preread

Example:

ok, err = kong.service.set_tls_verify(true)

if not ok:

# do something with error

Parameters:

on (bool) – Whether to enable TLS certificate verification for the current request

Returns:

true if the operation succeeded, nil if an error occurred

Return type:

bool

Returns:

An error message describing the error if there was one

Return type:

str

static set_tls_verify_depth(depth: number) Tuple[bool, str][source]

Sets the maximum depth of verification when validating upstream server’s TLS certificate. This call affects only the current request. For the depth to be actually used the verification has to be enabled with either the [proxy_ssl_verify](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_verify) directive or using the [kong.service.set_tls_verify](#kongserviceset_tls_verify) function.

Phases:

rewrite, access, balancer, preread

Example:

ok, err = kong.service.set_tls_verify_depth(3)

if not ok:

# do something with error

Parameters:

depth (number) – Depth to use when validating. Must be non-negative

Returns:

true if the operation succeeded, nil if an error occurred

Return type:

bool

Returns:

An error message describing the error if there was one

Return type:

str

static set_upstream(host: str) Tuple[bool, str][source]

Sets the desired Upstream entity to handle the load-balancing step for this request. Using this method is equivalent to creating a Service with a host property equal to that of an Upstream entity (in which case, the request would be proxied to one of the Targets associated with that Upstream). The host argument should receive a string equal to the name of one of the Upstream entities currently configured.

Phases:

access

Example:

ok, err = kong.service.set_upstream(“service.prod”)

if not ok:

kong.log.err(err)

return

Parameters:

host (str)

Returns:

true on success, or nil if no upstream entities where found

Return type:

bool

Returns:

An error message describing the error if there was one.

Return type:

str