When a visitor submit a form, I'd like to assoc to inputs his IP adress.
(POST "/form" {params :params} (assoc params :ip-address the-ip)
How to do this?
Thought of doing this:
(POST "/form" {params :params
client-ip :remote-addr}
(->> params keywordize-keys (merge {:ip-address client-ip}) str))
But this returns {... :ip-address "0:0:0:0:0:0:0:1"}
From Arthur Ulfeldt's comment to Bill's answer, I guess 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 :remote-addr
from the request object is typically the correct way to go unless you're sitting behind a load balancer. In that case your load balance may add a header e.g. x-forwarded-for
to the request. Which would make something like this appropriate.
(or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With