Tags down


How can i get the conn struct after Phoenix controller action

By : J.Raney
Date : September 17 2020, 03:00 AM
this will help I want to send some conn info to my Elasticsearch after all controller actions of my project, example: The controller action response, the request params and the endpoint. , I got it creating a plug in my application:
code :
defmodule MyAppWeb.Plugs.RequestLogger do
  @moduledoc false

  alias Plug.Conn

  def init(opts), do: opts

  def call(conn, _opts) do
    IO.puts("Getting conn after all controller actions here " <> Integer.to_string(conn.status))
defmodule MyAppWeb.Endpoint do
  # ...
  plug MyAppWeb.Router
  plug MyAppWeb.Plugs.RequestLogger

Share : facebook icon twitter icon

How to access 'conn' in the Controller module outside an action

By : Brian O
Date : March 29 2020, 07:55 AM
it should still fix some issue You can't. The code in the module is evaluated once at compilation. conn is a unique value that's created for each request and passed to the action.
You can override the action/2 function to extract and pass the locale to each action:
code :
def action(conn, _) do
  args = [conn, conn.params, get_session(conn, :locale)]
  apply(__MODULE__, action_name(conn), args)
def news(conn, _params, locale) do
  render(conn, "news.html", locale: locale)"
def news(conn, _params) do
  render(conn, "news.html", locale: locale(conn))"

def locale(conn), do: get_session(conn, :locale)

Phoenix: How to get conn %Plug.Conn{} in the console

By : Ryan
Date : March 29 2020, 07:55 AM
it helps some times Router helper functions accept either a conn or an endpoint module as the first argument. You can pass the endpoint module of your app when you want to generate a URL without a conn:
code :
MyAppWeb.Router.Helpers.confirmation_url(MyAppWeb.Endpoint, :edit, "12345")
conn = %Plug.Conn{private: %{phoenix_endpoint: MyAppWeb.Endpoint}}
MyAppWeb.Router.Helpers.confirmation_url(conn, :edit, "12345")

Elixir/Phoenix - Accessing user's cookie: conn.cookies vs conn.req_cookies vs conn.req_headers

By : sunshine1988
Date : March 29 2020, 07:55 AM
I wish did fix the issue. conn.cookies the request cookies with the response cookies conn.req_cookies the request cookies (without the response ones), of course there is coon.resp_cookies for the response cookies.
List.keyfind(conn.req_headers, "cookie", 0) just get the cookies from request's headers, which is conn.req_cookies.

expected :action/2 to return a Plug.Conn error in Phoenix controller

By : MA Rahman
Date : March 29 2020, 07:55 AM
I wish this help you You have an action in your controller that is not returning the conn. All actions must receive conn and return the updated conn.

How to read small chunks of data in phoenix controller, using Plug.Conn

By : Dagada
Date : March 29 2020, 07:55 AM
seems to work fine Plug.Conn.read_body/2 only reads one chunk of the request body. You need to call it recursively in order to read everything. You also don't need to write a Parser to just read the body in chunks (I don't think a Parser can even do that if I understand your question correctly); if the Content-Type of the request is not one that Plug parses by default, you can call Plug.Conn.read_body/2 from your controller.
Here's a small implementation of recursively calling Plug.Conn.read_body/2 from a controller:
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk