How to enforce JSON encoding for Phoenix Request?
If somebody will google for that and want to implement such behaviour.
lib/%APP_NAME%/endpoint.ex
plug Plug.Head
# add custom plug
plug :set_format, "json"
Define it:
defp set_format(conn, format) do
if Regex.match?(~r/^api.*/, conn.host) do
Plug.Conn.put_private conn, :phoenix_format, format
else
conn
end
end
In this example we have dirty hack which will enforce JSON format for subdomain api
It's not recommended to do like that but since Phoenix enforcing HTML at any time this hack fix ridiculous behaviour like: Elixir.Phoenix.Router.NoRouteError for pipeline :api
to show 404.json instead of 404.html