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
2
votes
1 answer

How does Plug.Exception work?

I’m having some trouble getting a simple example to work. I’m not using Phoenix FWIW, just plug defmodule Unauthorized do defexception message: "not authorized", plug_status: 401 end defmodule Foo do use Plug.Router plug :match plug…
luk3thomas
  • 2,512
  • 1
  • 18
  • 20
2
votes
1 answer

Access result of Elixir Plug.Parsers.JSON

I am writing a small http server using Elixir Plug and having some trouble understanding how its parser works. I cannot figure out how to access the result of the parser in my router. I currently have ... plug Plug.Parsers, parsers: [:json], …
BrendanM
  • 383
  • 3
  • 14
2
votes
0 answers

Setting Plug.Static Headers in Phoenix

I'm trying to put the header Connection: Keep-Alive in lib/app/endpoint.ex: plug Plug.Static, at: "/", from: :app, gzip: true, only: ~w(css fonts images js favicon.ico robots.txt), etag_generation: {}, cache_control_for_etags: "public,…
kayne
  • 349
  • 2
  • 15
1
vote
1 answer

How would I allow CORS with plug_cowboy in Elixir?

I'm trying to access these endpoints by making API calls elsewhere, how can I allow CORS for this? I'm running this on localhost:4001, and making the API calls from localhost:3000 (react). Thanks in advance. If you need any extra info (or files),…
Ewan H
  • 17
  • 4
1
vote
1 answer

Phoenix - Callback action between controller and view

I am building an admin tool app for our system. I want to record every action made by every user. Here's what I did defmodule AdminToolWeb.UserController do use AdminToolWeb, :controller ... def delete(conn, %{"id" => id}) do …
Dyey517
  • 426
  • 5
  • 15
1
vote
0 answers

ohmyzsh - Not an editor command: Plug

I am trying to setup my terminal. my .vimrc file was working without issue and I was able to see all the plugins. However after I installed ohmyzsh I am getting following error. E492: Not an editor command: Plug 'vim-airline/vim-airline' This is…
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
1
vote
2 answers

How Can I use 2 authentication plugs in Phoenix?

I have two users, Buyer and Admin in my app, they both have different login details, admin logs in with email and password, buyer logs in with their phone number and a code we send them. So, I made two authentication plugs. Here is how I have added…
Linda Kadz
  • 329
  • 2
  • 17
1
vote
1 answer

Do I have to do anything to make Plugs work in test environment (Elixir / Phoenix)?

I have a basic application and tried to use plugs on my controller. It should basically return 400 if the request body doesn't have "url" parameter. It works fine when I send a request from Postman but it seems it doesn't work on my tests. Do I…
htunc
  • 455
  • 5
  • 19
1
vote
2 answers

What's the correct way to respond to error page in a POST action without redirect in Phoenix?

I think it's a basic situation. I have a POST action, and I want to respond a 403 status and show the error page. def signup(conn, params) do ... conn |> Plug.Conn.send_resp(:forbidden, "Forbidden") |> Plug.Conn.halt() end However, it will…
Stephen
  • 3,822
  • 2
  • 25
  • 45
1
vote
0 answers

Elixir - plug is being triggered in other actions in Phoenix controller

I'm using PolicyWonk for authorization and I created this policy: def policy( %{current_user: current_user, organisation: organisation} = _assigns, :organisation_view ) when not is_nil(current_user) and not is_nil(organisation) do …
Bargain23
  • 1,863
  • 3
  • 29
  • 50
1
vote
1 answer

Redirect after POST using plug_cowboy (2.0)

I got a simple Plug post handler like this post "/some/url" do # do something # render(something) end …but I would like to redirect somehow to another get handler, instead of rendering html. How to do this using plug_cowboy 2.0?
gunnar2k
  • 185
  • 1
  • 13
1
vote
1 answer

How do I make Plug.ErrorHandler and other Plug work at the same time?

I noticed that the response generated by send_resp in the handle_errors callback of Plug.ErrorHandler does not pass through other Plugs. For example, I build a JSON response in handle_error. But this JSON response won't pass my JSONHeaderPlug and…
Hentioe
  • 228
  • 1
  • 9
1
vote
0 answers

How to test a Plug that sets a 404 status along with a Phoenix Redirect

I'm trying to test that the plug below returns a 404 when a site_id is not found. Everything works in the browser but the test throws an exception. My plug: defmodule MyWeb.Plugs.GetSite do import Plug.Conn def init(default), do: default def…
Kristian Roebuck
  • 3,209
  • 2
  • 21
  • 29
1
vote
1 answer

Phoenix 1.4 How to alias Routes.page_path in a Plug

I've tried a couple different iterations but I keep getting this error when compiling: Routes.session_path/2 is undefined (module Routes is not available) My Code: defmodule Blackbook.Plugs.RequireAuth do import Plug.Conn import…
Nathan
  • 239
  • 3
  • 15
1
vote
1 answer

How to do Plug pipelines in Elixir?

Background I have an app that is a webserver with cowboy and uses Plugs. Since this app was inherited, using Phoenix is out of the question unless we remake the entire thing, which is not happening. My objective is to instead of having everything…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266