how to get the remote ip (requester) on grape-api rails application
Asked Answered
M

2

9

I have a working rails application with grape-gem working as an end point for some APIs in the application. I need to get the remote ip for the requester and return it back in the response. I could do that on regular controllers using

request.remote_ip

however, in grape calls, the 'request' is not a defined variable.

how can I get access to the remote ip on grape?

Marou answered 6/12, 2013 at 3:7 Comment(1)
github.com/ruby-grape/grape#remote-ipMillepede
S
16

Use env hash:

env['REMOTE_ADDR']
Sherrylsherurd answered 6/12, 2013 at 3:41 Comment(0)
K
1

The 'answer' by @zx1986 is the best / correct. REMOTE_ADDR is not safe, as outlined in this effort-post answer. The Grape API docs recommend using the direct remote_ip value (for ActionDispatch) in the request env, when the request helper is obscured, like in the case of Grape:

env["action_dispatch.remote_ip"]
# or
request.env["action_dispatch.remote_ip"]
Knitting answered 13/7, 2022 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.