HTTP response
The nauthilus_http_response module allows Lua code to set or modify HTTP response headers,
set the HTTP status code, and write the raw response body. This is useful, for example, to
signal frontends (like Keycloak) to apply additional protection measures (e.g., CAPTCHA),
return specific HTTP statuses from custom hooks, or serve non-JSON content directly from Lua.
Availability: since Nauthilus 1.8.5. Status and body writing support added in 1.9.0. Content-Type helper and Gin-mapped wrappers added in 1.9.2.
local nauthilus_http_response = require("nauthilus_http_response")
Notes
- Availability: This module is available in Hooks. It may also be used by Filters and Features, but with strict limitations (see below). It is not available in non-HTTP contexts (e.g., pure backend workers without an HTTP response).
- If you write generic Lua code that may also run in non-HTTP contexts, guard calls with
pcall(...). - Header names are case-insensitive on the wire; use canonical forms for readability.
- Important: Filters and Features MUST NOT send a response body. They may set or add/remove HTTP response headers to signal state to the frontend, and they may set an HTTP status code if appropriate, but they must not write the response body or use helpers that emit a body (see Prohibited operations in Filters/Features).
nauthilus_http_response.set_http_response_header
Replaces the value of an HTTP response header. If the header already exists, its values are overwritten with the provided one.
Syntax
nauthilus_http_response.set_http_response_header(name, value)
Parameters
name(string): Header namevalue(string): Header value to set
Returns
None
Example
local nauthilus_http_response = require("nauthilus_http_response")
-- Force JSON response type
nauthilus_http_response.set_http_response_header("Content-Type", "application/json")
nauthilus_http_response.add_http_response_header
Appends a value to an HTTP response header without removing existing values.
Syntax
nauthilus_http_response.add_http_response_header(name, value)
Parameters
name(string): Header namevalue(string): Header value to add
Returns
None
Example
local nauthilus_http_response = require("nauthilus_http_response")
-- Add an additional Vary entry while keeping others intact
nauthilus_http_response.add_http_response_header("Vary", "Accept-Encoding")
nauthilus_http_response.remove_http_response_header
Removes an HTTP response header from the response.
Syntax
nauthilus_http_response.remove_http_response_header(name)
Parameters
name(string): Header name
Returns
None