user7040064
user7040064

Reputation:

How to get an IP address of a client?

I need to get an IP address of a client, this doesn't work:

  def create(conn) do
    ip_address = conn.inet.ip_address
    # ....

due to key :inet not found in: %Plug.Conn. How can I get an IP address then?

Upvotes: 14

Views: 8582

Answers (2)

mcherep
mcherep

Reputation: 391

The get the IP:

conn.remote_ip

Casting from ip_address to string:

to_string(:inet_parse.ntoa(conn.remote_ip))

Upvotes: 29

TheAnh
TheAnh

Reputation: 2813

Check this Request fields:

remote_ip - the IP of the client, example: {151, 236, 219, 228}. This field is meant to be overwritten by plugs that understand e.g. the X-Forwarded-For header or HAProxy’s PROXY protocol. It defaults to peer’s IP.

This is what you're looking for:

conn.remote_ip

Upvotes: 16

Related Questions