I've inherited a project written in Elixir. Basically it's a API gateway. In this case, I need to perform a redirect from my API Gateway URL to the one which the destination URL redirects to. So the situation is like this.
Request to (API Gateway in Elixir) > goes to my server, which returns a 302 and redirects to another URL.
I've written some code like this:
def index(%{query_string: query_string} = conn, data) do
final_conn =
case MyLib.index(data, [], query_string) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
conn
|> put_status(200)
|> put_resp_content_type("application/vnd.api+json")
|> json(body)
{:ok, %HTTPoison.Response{status_code: 500, body: body}} ->
conn
|> put_status(500)
|> put_resp_content_type("application/vnd.api+json")
|> json(body)
{:ok, %HTTPoison.Response{status_code: 302, body: body}} ->
conn
|> put_status(302)
|> redirect(to: conn)
|> Plug.Conn.halt()
{:ok, %HTTPoison.Response{status_code: 303, body: body}} ->
conn
|> put_status(303)
|> redirect(to: conn)
|> Plug.Conn.halt()
{:ok, %HTTPoison.Response{status_code: 404}} ->
conn
|> put_status(404)
|> put_resp_content_type("application/vnd.api+json")
|> json(%{error_code: "404", reason_given: "Resource not found."})
{:error, %HTTPoison.Error{reason: reason}} ->
conn
|> put_status(500)
|> put_resp_content_type("application/vnd.api+json")
|> json(%{error_code: "500", reason_given: reason})
end
final_conn
where MyLib.index is simply
def index(conn, headers \\ [], query_string) do
HTTPoison.get(process_url("?#{query_string}"), headers)
end
This code correctly manage the error parts, but I'm not able to make it work for 301 or 302.
(It goes without saying, I've never seen Elixir in my life).
redirect(to: conn)
meant to do? I am used to seeingredirect(to: "some_actual_url.xyz")
- Peaceful James:follow_redirect
option to true in yourHTTPoison.get/3
call, e.g.HTTPoison.get(process_url("?#{query_string}"), headers, follow_redirect: true)
. (I think HTTPoison doesn't follow redirects by default) - Everett