Questions tagged [plug]

Plug is a web application framework for Elixir.

An example of a Plug from the module documentation:

Hello World

defmodule MyPlug do
  import Plug.Conn

  def init(options) do
    # initialize options
    options
  end

  def call(conn, _opts) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")
  end
end

The snippet above shows a very simple example on how to use Plug. Save that snippet to a file and run it inside the plug application with:

iex -S mix
iex> c "path/to/file.ex"
[MyPlug]
iex> {:ok, _} = Plug.Cowboy.http MyPlug, []
{:ok, #PID<...>}
101 questions
9
votes
3 answers

Elixir / Phoenix: How to implement session timeout / expiration

I'm working on a vanilla Elixir / Phoenix application and followed the general steps in the Programming Phoenix book to implement a basic sign in & sign out system (see snippets below). However I see no advice in the book or online about how to set…
Topher Hunt
  • 4,404
  • 2
  • 27
  • 51
8
votes
3 answers

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

After iex -S mix phx.server I want to do some quick tests in the iex terminal, but some functions require the struct %Plug.Conn{} as an argument, for example I wanted to get the result of…
AndreyKo
  • 1,421
  • 2
  • 11
  • 25
8
votes
2 answers

Read the raw body from a plug connection after parsers in Elixir

I have a requirement to check the digest of JSON content sent to a phoenix server. To check the digest the raw body is needed. Is there any way to access the raw content in a plug later in the pipeline than the parsers. I want to add the following…
Peter Saxton
  • 4,466
  • 5
  • 33
  • 51
5
votes
2 answers

Phoenix started throwing (UndefinedFunctionError) function :crypto.rand_bytes/1 is undefined or private

After and update to my system - MAC, my phoenix app compile just fine but throw this error any time I hit any route. Server: localhost:4000 (http) Request: GET / ** (exit) an exception was raised: ** (UndefinedFunctionError) function…
Renews
  • 614
  • 5
  • 17
5
votes
1 answer

Phoenix framework pattern match request headers

I'm trying to implement a custom plug for API versioning. Now I need to be able to match a value passed with the requests headers (i.e. Accept: application/vnd.app.v1+json). So far I have implemented the following: defmodule…
tomasbasham
  • 1,695
  • 2
  • 18
  • 37
5
votes
2 answers

Why is it useful to convert HEAD requests to GET requests?

In a new Phoenix app the Plug.Head plug is present by default and I was intrigued about its significance. I know that "the HEAD method is identical to GET except that the server MUST NOT send a message body in the response". I think the official…
toraritte
  • 6,300
  • 3
  • 46
  • 67
4
votes
2 answers

Is there a way to have a Phoenix Plug just for one route?

In Phoenix I have my routes as follow : scope "/", ManaWeb do pipe_through [:browser, :auth] get "/register", RegistrationController, :new post "/register", RegistrationController, :register end However I would like to set a Plug…
thodg
  • 1,773
  • 15
  • 24
4
votes
1 answer

HMAC, Elixir, Plug.Conn (trying to call read_body more than once)

I'm struggling with an issue where something is reading the body of an http request before Plug.Parsers.JSON gets it in the pipeline. Because of this, read_body in the plug for json times out--you can't read the body twice. We have an HMAC…
jaydel
  • 14,389
  • 14
  • 62
  • 98
4
votes
2 answers

how to redirect traffic from Http to Https in Phoenix Elixir for SSL on Load Balancer?

I need to forward the traffic from Http to Https, SSL is on the load balancer so all I need to do is forward the header by using the plug_ssl so in my prod.exs I added: config :web, Web.Endpoint, force_ssl: [rewrite_on: [:x_forwarded_proto]] in…
Mr H
  • 5,254
  • 3
  • 38
  • 43
4
votes
1 answer

Email templates using bamboo without phoenix

I am working on app in elixir. It sends email to clients. I am using bamboo library for sending emails. So far, emails are working fine. But now, I am trying to send emails using templates. Everywhere i see in the bamboo documentation is using…
NeiL
  • 791
  • 8
  • 35
3
votes
1 answer

Malicious request (via invalid URI) causes exception to be thrown in Elixir Phoenix/Plug

I just got an alert from Sentry from my app running in production that seems to result from a malicous request. I’ve managed to recreate the error in my local env: (Plug.Router.MalformedURIError) malformed URI…
harryg
  • 23,311
  • 45
  • 125
  • 198
3
votes
1 answer

Write custom Plug which can handle and return proper error on malformed JSON in the body

I am trying to write a plug which will generate a custom error if the request has malformed JSON which is quite often the case in our scenarios(as we use variables in postman. eg sometimes there is no quote outside the value and it results in…
Tanweer
  • 567
  • 1
  • 5
  • 17
3
votes
1 answer

How to populate request parameters into a Plug.Conn connection?

I'm trying to test a method that receives a connection of type Plug.Conn but I don't find a way of initializing the connection with the request parameters with the Plug.Conn API. E.g: test "put request params", %{conn: conn} do # put %{"foo" =>…
carpamon
  • 6,515
  • 3
  • 38
  • 51
3
votes
2 answers

What is the best way to use functions of Controller and Conn in Plug Module?

I want to write my own plug in my phoenix app. The plug is to check the cookies and render the error page if cookies don't exist. Similar to the 404 error. Based on the logic, those functions below may be called: put_status: set the status code of…
Stephen
  • 3,822
  • 2
  • 25
  • 45
3
votes
2 answers

Versioning API in Elixir Plug

I have two modules: lib/endpoints/v1/base.ex and lib/endpoints/v2/base.ex. lib/endpoints/v1/base.ex defmodule Http.Endpoints.V1.Base do require Logger use Plug.Router plug(:match) plug(:dispatch) plug(Plug.Logger) plug(Plug.Parsers,…
Flavio Milan
  • 467
  • 3
  • 16
1
2 3 4 5 6 7