Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 830
owyongsk Avatar asked May 12 '16 03:05

owyongsk


2 Answers

You can use get_req_header/2. For example

get_req_header(conn, "referer")
like image 164
Dennis Avatar answered Sep 20 '22 14:09

Dennis


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
like image 34
Dogbert Avatar answered Sep 21 '22 14:09

Dogbert