views:

703

answers:

1

This is a really simple question, but I cannot find any mention of this, anywhere..

How do I get the client's IP address from in Sinatra?

get '/' do
    "Your IP address is #{....}"
end
+4  A: 

I was coming to post the answer anyway.. so:

get '/' do
"Your IP address is #{ @env['REMOTE_ADDR'] }"
end

Sinatra uses the Rack::Request API, so you can use a lot of things available in it.
Also a link to the Sinatra doc's.

Brian Gianforcaro
Hm, the Rack::Request API mentions an `ip` method, which handles the `HTTP_X_FORWARDED_FOR` as well, is there a way to call this from Sinatra?
dbr
you should be able to just do #{ @env['HTTP_X_FORWARDED_FOR'] } I have never tested this though, so I'm not positive.
Brian Gianforcaro
you can do request.ip directly, as well. get "/" do; "your IP: #{request.ip}"; end
bantic