How could I revamp response after controller?

# 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 ?

There’s Plug.Conn.register_before_send/2, but you need to be aware that send_resp/3 is not a pure function and it does send the response to the client as a sideeffect. Therefore even this might actually be to late for what you want to do.

It sounds like what you want is to render a json view. You can read more about it in the Phoenix guides https://hexdocs.pm/phoenix/views.html#rendering-json