Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix Framework: How to Route Custom Media Type?

In the Phoenix Framework, how does one route a custom media type in Accepts?

Phoenix's own code comments indicate the following is all that is necessary—plus a recompile of deps, though the need for that escapes me. But, this seems not to work:

config.exs:

[…]

config :plug, :mimes, %{
  "application/vnd.api+json" => ["json-api"]
}

router.ex:

pipeline :api do
  plug :accepts, ["json-api"]
end

[…]

scope "/", SomeApp do
  pipe_through :api

  […]

some_test.ex:

setup do
  conn = conn() |> put_req_header("accept", "application/vnd.api+json")
  {:ok, conn: conn}
end

All tests' requests (using conn from setup) receive HTTP 406 responses.

like image 965
Yuri Gadow Avatar asked Sep 25 '15 14:09

Yuri Gadow


1 Answers

Turns out that the following is inadequate:

% touch deps/plug/mix.exs
% mix deps.compile plug
% mix clean

Instead, as @josé-valim suggests in the question's comments, deleting the entire _build directory did the trick. I went back and forth a few times to be sure, and each time I only touched and deps.compiled, no joy, and each time I removed _build, joy.

like image 88
Yuri Gadow Avatar answered Nov 15 '22 09:11

Yuri Gadow