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.

4
  • 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 Sep 25, 2015 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, 2015 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. Sep 25, 2015 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, 2015 at 23:01

1 Answer 1

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.

1
  • Thank you. I spent a long time trying to figure out how to run tests with ja_serializer.
    – Nick Eaket
    Dec 28, 2015 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.