2 title: "Phoenix: Plug.Conn"
16 conn.host # → "example.com"
18 conn.path_info # → ["posts", "1"]
19 conn.request_path # → "/posts/1"
20 conn.query_string # → "utm_source=twitter"
23 conn.peer # → { {127, 0, 0, 1}, 12345 }
24 conn.remote_ip # → { 151, 236, 219, 228 }
25 conn.req_headers # → [{"content-type", "text/plain"}]
29 conn |> get_req_header("content-type")
37 |> put_req_header("accept", "application/json")
40 Usually only useful for tests.
49 conn.resp_body # → "..."
50 conn.resp_charset # → "utf-8"
51 conn.resp_cookies # → ...
52 conn.resp_headers # → ...
61 |> html("<html><head>...")
62 |> json(%{ message: "Hello" })
67 |> redirect(to: "/foo")
68 |> redirect(external: "http://www.google.com/")
73 |> put_resp_content_type("text/plain")
74 |> put_resp_cookie("abc", "def")
75 |> put_resp_header("X-Delivered-By", "myapp")
77 |> put_status(:not_found)
81 |> put_private(:plug_foo, "...") # reserved for libraries
93 |> render("index.html")
94 |> render("index.html", hello: "world")
95 |> render(MyApp.ErrorView, "404.html")
101 |> put_view(ErrorView)
105 |> put_secure_browser_headers()
106 # prevent clickjacking, nosniff, and xss protection
107 # x-frame-options, x-content-type-options, x-xss-protection
111 |> put_new_view(ErrorView) # if not set yet
112 |> put_new_layout(:foo)
126 conn.assigns # storage of crap
128 conn.halted # if pipeline was halted
129 conn.secret_key_base # ...
130 conn.state # :unset, :set, :file, :sent, :chunked
136 plug :accepts, ["html", "json"]
137 conn |> accepts(["html", "json"])
138 get_format(conn) # → "html"
146 conn |> assign(:user_id, 100)
150 conn = async_assign(conn, :location, fn -> geoip_lookup() end)
151 await_assign(conn, :location)
157 conn = fetch_session(conn) # or plug :fetch_session
159 conn = put_session(conn, :message, "new stuff we just set in the session")
160 get_session(conn, :message)
161 conn = clear_session(conn)
166 |> put_flash(:info, "Success")
167 |> put_flash(:error, "Oh no")
170 Also available: `flash` `cookie` `params`