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

Plug.Parser not reading/parsing JSON body

I am coding an Elixir (1.8) + Plug_Cowboy (2.0.2) + Jason (1.1.2) server. From what I am getting from the documentation, once the Plug parser has passed, I should have everything in body_params. The problem is that accessing conn.body_params in my…
Kamen
  • 3,575
  • 23
  • 35
3
votes
1 answer

RewriteUrl in Phoenix / Plug / Cowboy?

How to rewrite an Url in phoenix? For example rewrite all requests to //www.app.com/xyz to //app.com/xyz Is there an easy option for it, like force_ssl? Has anyone an idea, where to PLUG it in? Has plug an option for it?
webdeb
  • 12,993
  • 5
  • 28
  • 44
2
votes
0 answers

Phoenix custom error view doesn't send in assigns?

I'm trying to access the assigns after a Custom Error View renders an error caught by the phoenix server. My problem is that the assigns attributes are not coming over. My specific issue is that the functions used for home page navigation think the…
tblev
  • 422
  • 4
  • 13
2
votes
1 answer

How to access file uploads / POST in Elixir / Plug

There's a bunch of information on how to do this with Phoenix, but I'm purposefully avoiding using Phoenix until I learn more about how Elixir works. To that end, I have the following Plug.Router path: defmodule ElixirHttpServer do use…
diplosaurus
  • 2,538
  • 5
  • 25
  • 53
2
votes
1 answer

With block in Plug server throws MatchError instead of using else block

Previously, I would use with blocks in Elixir Plug servers to parse parameters from requests and return a sane response if that failed. However, that doesn't seem to be working anymore (Elixir 1.11). Can anyone point out what's going on? Here is a…
Daniel Drexler
  • 527
  • 4
  • 14
2
votes
0 answers

How to fix ERR_UNSAFE_PORT in android chrome

I have a super basic http server made with plug_cowboy in elixir. When I send a request with curl, it works as expected, but when I do that in google chrome, it shows a ERR_UNSAFE_PORT error. This has never happened with a nodejs or python…
faBri_CI-o
  • 53
  • 1
  • 6
2
votes
1 answer

Validating headers for specific endpoints with Plug

Background I have a simple Plug router that has some POST, PUT and GET endpoints. However, each endpoint needs to make a specific validation on the headers of the request. I don’t know how to achieve this with Plug. Code I have a very generic router…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
2
votes
1 answer

How do you run middleware functions post response in Phoenix framework?

I'm developing a simple website in Elixir with Phoenix. I'd like to add some custom middleware that runs after a response has been generated. For example, in order to log the total number of bytes in each response I'd like to have a Plug like…
Segfault
  • 8,036
  • 3
  • 35
  • 54
2
votes
1 answer

Serving static html (and others) from / in Phoenix Framework

I'm working on an API server implemented with the Phoenix Framework. JSON APIs are served from the /api/ URL. The server also needs to serve static .html (and others, .css, .js, etc.) files from / : it's actually a little SPA developed in…
mszmurlo
  • 1,250
  • 1
  • 13
  • 28
2
votes
2 answers

How to write a test for Plug error handling

I'm trying to use Plug.Test to test error handling implemented with Plug.ErrorHandler -- with assert conn.status == 406 and alike. I have the defp handle_errors (containing a single send_resp statement) and it seems to be called, however, my tests…
Costa Shapiro
  • 524
  • 1
  • 3
  • 8
2
votes
1 answer

How can i get the conn struct after Phoenix controller action

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. What can we do if we need to work with the conn struct returned by the…
2
votes
2 answers

Does a Plug router need a match/dispatch pipeline?

I have a Router module that forwards request to other routers. In this router I have a pipleline comprised of plug(:match) and plug(:dispatch). defmodule Example.Router do use Plug.Router plug(:match) plug(:dispatch) forward("/check",…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
2
votes
1 answer

File Upload to Local using Acr in Elixir

I am using Arc.Definition(https://github.com/stavro/arc) for uploading an image to the Local Storage. My file_service.ex is below: defmodule MyApp.FileService do use Arc.Definition use Arc.Ecto.Definition @image_types ~w(.jpg .jpeg .png…
Tessy Thomas
  • 1,475
  • 12
  • 19
2
votes
1 answer

Crypto Exception in Elixir

I have configured an elixir app and defined admin routes. But, I am getting the following error while trying to access any routes. (exit) an exception was raised: ** (ArgumentError) argument error (crypto) :crypto.hmac_nif(:sha256,…
Tessy Thomas
  • 1,475
  • 12
  • 19
2
votes
1 answer

Parsing a json part of a multipart/mixed request in Elixir/Phoenix

I'm writing an Elixir/Phoenix json API backend and would like an endpoint to receive multiple image files along with some json-formatted attributes in a single request. I'm using the multipart/mixed content type for that and here is what my cURL…
Nic Nilov
  • 5,056
  • 2
  • 22
  • 37