PlugRequireHeader
An Elixir Plug for requiring and extracting a given header.
Summary↑
call(conn, options) | Extracts the required headers and assigns them to the connection struct |
init(options) | Initialises the plug given a keyword list |
Functions
Extracts the required headers and assigns them to the connection struct.
Arguments
conn
- the Plug.Conn connection struct
options
- a keyword list broken down into mandatory and optional options
Mandatory options
:headers
- a keyword list of connection key and header key pairs.
Each pair has the format [<connection_key>: <header_key>]
where
- the
<connection_key>
atom is the connection key to assign the value of the header. - the
<header_key>
binary is the header key to be required and extracted.
Optional options
:on_missing
- specifies how to handle a missing header. It can be one of
the following:
- a callback function with an arity of 2, specified as a tuple of
{module, function}
. The function will be called with theconn
struct and a tuple consisting of a connection assignment key and header key pair. Notice that the callback may be invoked once per required header. - a keyword list with any or all of the following keys set.
:status
- aninteger
oratom
to specify the status code. If it’s an atom, it’ll be looked up using thePlug.Status.code
function. Default is:forbidden
.:message
- abinary
sent as the response body. Default is an empty string.:as
- anatom
describing the content type and encoding. Currently supported alternatives are:text
for plain text and:json
for JSON. Default is:text
.
If setting options instead of using a callback function, notice that the plug pipeline will always be halted by a missing header, and the configured response will only be sent once
Initialises the plug given a keyword list.