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"}
{... :ip-address "0:0:0:0:0:0:0:1"}
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)))
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.
:remote-addr
x-forwarded-for
(or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request))
, . ring.middleware.dump(handle-dump). , -, , . IP- . http, , , , - json , . , , , .