# suppose this is one of my controllers
defmodule MyController do
use Phoenix.Controller
def my_action(conn, _) do
res =
case something_process do
once -> %{data: "some data"}
otherwise -> %{error: "some error"}
end
json(conn, res)
end
end
Now I want to inject some custom revamp the response
such as ‘%{data: “old data”}’ to ‘%{data: “new data”, new_filed_need_to_add_gloabl: “value”}’
# I have tried the plug such as
defmodule MyController do
use Phoenix.Controller
plug :action
plug :custom_revamp
def my_action(conn, _) do
res =
case something_process do
once -> %{data: "some data"}
otherwise -> %{error: "some error"}
end
json(conn, res)
end
def custom_revamp(conn, _), do: "some processing"
end
Even it will do my_action first, then go through custom_revamp.
But I could not get the response_body from the conn.
And I also try to re-write action such as
def action(conn, _) do
args = [conn, conn.params]
apply(__module__, action_name(conn), args)
|> custom_revamp()
end
end
But It couldn’t get the response_body too.
So how could I do ?