PlugRequireHeader v0.8.1 PlugRequireHeader

An Elixir Plug for requiring and extracting a given header.

Summary

Functions

Extracts the required headers and assigns them to the connection struct

Initialises the plug given a keyword list

Functions

call(conn, options)

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 the conn 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 - an integer or atom to specify the status code. If it’s an atom, it’ll be looked up using the Plug.Status.code function. Default is :forbidden.
  • :message - a binary sent as the response body. Default is an empty string.
  • :as - an atom 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

init(options)

Initialises the plug given a keyword list.