Reputation: 340
I would like to act as a proxy for certain requests in my application. For example: if a user requests the link "http://myapp/proxy" , I would like to forward the request to an external website and retrieve the response to the user. The problem here is that I don't want to do URL redirection, but I would like to forward the client requests, acting on behalf of the user.
How can I do it using Phoenix framework?
Upvotes: 4
Views: 2305
Reputation: 888
For the sake of completeness, here's what i did:
case HTTPoison.get(url) do
{ :ok, response } -> conn |> put_resp_content_type(response.headers["Content-Type"]) |> send_resp(response.status_code, response.body)
{ :error, _error } -> conn |> put_status(:bad_gateway) |> render(AssetsProxy.ErrorView, "502.html")
end
still very new to the whole elixir/phoenix party, so i'm sure there's stuff to improve on. e.g. it does not handle redirects correctly.
Upvotes: 5
Reputation: 340
To "return" the data back to the client, the code is the following:
HTTPoison.start
response = HTTPoison.post!(url, body, [])
conn |> halt |>
html response.body
Upvotes: 0