Hi there,
I have a controller with an action that renders some html. Now I want to extend that action to render some json when the content type is application/json*
I was hoping I could use pattern matching to achieve this, but this does not work (the html action always gets executed):
def index(%{req_headers: [{"content-type", "application/json"}]} = conn, _params) do
json(conn, %{some: "response"})
end
def index(conn, _params) do
render(conn, "index.html")
end
This does work:
def index(conn, _params) do
headers = Enum.into(conn.req_headers, %{})
if headers["content-type"] == "application/json" do
json(conn, %{some: "response"})
else
render(conn, "index.html")
end
end
Is there a cleaner to achieve this?
Thanks!