How to get client IP address in ring-clojure?

When the visitor submits the form, I would like to associate with entering its IP address.

(POST "/form" {params :params} (assoc params :ip-address the-ip)

How to do it?

Thought of it:

(POST "/form" {params    :params
               client-ip :remote-addr} 
      (->> params keywordize-keys (merge {:ip-address client-ip}) str)) 

But it returns {... :ip-address "0:0:0:0:0:0:0:1"}

+4
source share
3 answers

From Arthur Ulfeld's comment to Bill's answer, I think we could write this:

(defn get-client-ip [req]
  (if-let [ips (get-in req [:headers "x-forwarded-for"])]
    (-> ips (clojure.string/split #",") first)
    (:remote-addr req)))
+4
source

Getting a :remote-addrrequest from an object is usually the right way if you are not sitting at a load balancer. In this case, your load balance may add a header, for example. x-forwarded-forto request. Which would make something like this suitable.

(or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request))
+4
source

, . ring.middleware.dump(handle-dump). , -, , . IP- . http, , , , - json , . , , , .

0

All Articles