You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 10, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+15-4Lines changed: 15 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,10 @@ defp deps do
15
15
end
16
16
```
17
17
18
-
Add the plug to e.g. a pipeline in a [Phoenix](http://www.phoenixframework.org/) controller. In this case we will require the request header `x-api-key` to be set, extract its first value and assign it the connection (a `Plug.Conn`) for later use in another plug or action.
18
+
Add the plug to e.g. a pipeline in a [Phoenix](http://www.phoenixframework.org/)
19
+
controller. In this case we will require the request header `x-api-key` to be set,
20
+
extract its first value and assign it the connection (a `Plug.Conn`) for later use
21
+
in another plug or action.
19
22
```elixir
20
23
defmoduleMyPhoenixApp.MyControllerdo
21
24
useMyPhoenixApp.Web, :controller
@@ -31,9 +34,14 @@ defmodule MyPhoenixApp.MyController do
31
34
end
32
35
end
33
36
```
34
-
Notice how the first value required header `"x-api-key"` has been extracted and can be retrieved using `conn.assigns[:api_key]`. An alternative is to use `Plug.Conn.get_req_header/2` to get all the values associated with a given header.
37
+
Notice how the first value required header `"x-api-key"` has been extracted
38
+
and can be retrieved using `conn.assigns[:api_key]`. An alternative is to use
39
+
`Plug.Conn.get_req_header/2` to get all the values associated with a given header.
35
40
36
-
By default, a missing header will return a status code of 403 (forbidden) and halt the plug pipeline, i.e. no subsequent plugs will be executed. The same is true if the required header is explicitly set to `nil`. This behaviour however is configurable.
41
+
By default, a missing header will return a status code of 403 (forbidden) and halt
42
+
the plug pipeline, i.e. no subsequent plugs will be executed. The same is true if
43
+
the required header is explicitly set to `nil` as the underlying HTTP server will
44
+
not include the header. This behaviour however is configurable.
37
45
```elixir
38
46
defmoduleMyPhoenixApp.MyOtherControllerdo
39
47
useMyPhoenixApp.Web, :controller
@@ -55,7 +63,10 @@ defmodule MyPhoenixApp.MyOtherController do
55
63
end
56
64
end
57
65
```
58
-
If the header is missing or set to `nil` the status code, a status code of 400 (bad request) will be returned before the plug pipeline is halted. Notice that the function specified as a callback needs to be a public function as it'll be invoked from another module.
66
+
If the header is missing or set to `nil` the status code, a status code of 400
67
+
(bad request) will be returned before the plug pipeline is halted. Notice that
68
+
the function specified as a callback needs to be a public function as it'll be
69
+
invoked from another module.
59
70
60
71
Lastly, it's possible to extract multiple headers at the same time.
0 commit comments