dbr
dbr

Reputation: 169693

Get client's IP address in Sinatra?

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

Upvotes: 39

Views: 18965

Answers (2)

user229044
user229044

Reputation: 239541

Sinatra provides a request object, which is the interface to the client request data that you should be using.

Using request.ip is the preferred method to find the client's IP address:

get '/' do
  "Your IP address is #{request.ip}"
end

Upvotes: 75

Brian Gianforcaro
Brian Gianforcaro

Reputation: 27220

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.

Upvotes: 18

Related Questions