5

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.

Yuri Gadow
  • 1,814
  • 2
  • 16
  • 26
  • You are mapping `"api-json"` in your config, but you are passing `"json-api"` to your accepts plug. Typo in the question or typo in your code? That seems like the likely culprit – Chris McCord Sep 25 '15 at 15:14
  • @ChrisMcCord Thanks for pointing that out, only a typo in the question I'm afraid, i.e., the code I tested doesn't have that issue and still doesn't work. Corrected the question. – Yuri Gadow Sep 25 '15 at 15:28
  • 1
    Can you specify the "does not seem to work?". What is not working? What were you expecting and what are you getting? Also, make sure your dependency was recompiled, `rm -rf _build` if in doubt. – José Valim Sep 25 '15 at 20:53
  • Sorry, that was a really lousy write up on my part. What I should have said (and have edited the Q to now say) is that doing the above was resulting in 406s. – Yuri Gadow Sep 25 '15 at 23:01

1 Answers1

7

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.

Yuri Gadow
  • 1,814
  • 2
  • 16
  • 26