kong.service.response

kong.service.response

Manipulation of the response from the Service

kong.service.response.get_status()

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

Phases

  • header_filter, body_filter, log

Returns

  • number|nil the status code from the response from the Service, or nil if the request was not proxied (i.e. kong.response.get_source() returned anything other than "service".

Usage

  1. kong.log.inspect(kong.service.response.get_status()) -- 418

Back to top

kong.service.response.get_headers([max_headers])

Returns a Lua table holding the headers from the response from the Service. 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 will only return headers that were present in the response from the Service (ignoring headers added by Kong itself). If the request was 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

Parameters

  • max_headers (number, optional): customize the headers to parse

Returns

  1. table the response headers in table form

  2. string err If more headers than max_headers were present, a string with the error "truncated".

Usage

  1. -- Given a response with the following headers:
  2. -- X-Custom-Header: bla
  3. -- X-Another: foo bar
  4. -- X-Another: baz
  5. local headers = kong.service.response.get_headers()
  6. if headers then
  7. kong.log.inspect(headers.x_custom_header) -- "bla"
  8. kong.log.inspect(headers.x_another[1]) -- "foo bar"
  9. kong.log.inspect(headers["X-Another"][2]) -- "baz"
  10. end

Back to top

kong.service.response.get_header(name)

Returns the value of the specified response header.

Unlike kong.response.get_header(), this function will only return a header if it was present in the response from the Service (ignoring headers added by Kong itself).

Phases

  • header_filter, body_filter, log

Parameters

  • name (string): 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

  • string|nil The value of the header, or nil if a header with name was not found in the response. If a header with the same name is present multiple times in the response, this function will return the value of the first occurrence of this header.

Usage

  1. -- Given a response with the following headers:
  2. -- X-Custom-Header: bla
  3. -- X-Another: foo bar
  4. -- X-Another: baz
  5. kong.log.inspect(kong.service.response.get_header("x-custom-header")) -- "bla"
  6. kong.log.inspect(kong.service.response.get_header("X-Another")) -- "foo bar"

Back to top

kong.service.response.get_raw_body()

Returns the raw buffered body.

Phases

  • header_filter, body_filter, log

Returns

  • string body The raw buffered body

Usage

  1. -- Plugin needs to call kong.service.request.enable_buffering() on `rewrite`
  2. -- or `access` phase prior calling this function.
  3. local body = kong.service.response.get_raw_body()

Back to top

kong.service.response.get_body([mimetype[, max_args]])

Returns the decoded buffered body.

Phases

  • header_filter, body_filter, log

Parameters

  • mimetype (string, optional): The mime-type of the response (if known)
  • max_args (number, optional): set a limit on the maximum number of parsed

Returns

  • string body The raw buffered body

Usage

  1. -- Plugin needs to call kong.service.request.enable_buffering() on `rewrite`
  2. -- or `access` phase prior calling this function.
  3. local body = kong.service.response.get_body()

Back to top