owyongsk
owyongsk

Reputation: 2469

Get HTTP_REFERRER from Plug.Conn

I developed a pixel tracking microservice using Elixir and Phoenix. I am trying to get the original URL where the pixel tracking is installed from Plug.Conn.

I assumed that I could try and get the Plug.Conn's HTTP_REFERRER header or variable or something but I must be wrong in maybe how the browser and HTTP works as I could not find anything about referrer in Plug.Conn in my controller.

Any ideas?

Upvotes: 7

Views: 3400

Answers (2)

Dennis
Dennis

Reputation: 59557

You can use get_req_header/2. For example

get_req_header(conn, "referer")

Upvotes: 14

Dogbert
Dogbert

Reputation: 222268

The referer is present in conn.req_headers. You can get it using List.keyfind/4:

case List.keyfind(conn.req_headers, "referer", 0) do
  {"referer", referer} ->
    IO.puts referer
  nil ->
    IO.puts "no referer"
end

Upvotes: 12

Related Questions