Although there used to be some methods to get client IP address (e.g. req.peerHostname
), I cannot figure out how to get it in Vapor 3.0.
Could anyone please let me know how to get client IP address in Vapor 3.0?
Thanks to the Vapor community, I have got an answer.
You can use req.http.remotePeer.hostname
for Vapor 3.0 project.
Hi hope this helps for Vapor 4 use this
func getIp(req: Request) throws -> EventLoopFuture<String> {
print(req.headers.forwarded.first!.for)
return req.eventLoop.makeSucceededFuture("\(req.headers.forwarded.first!.for ?? "Not found")")
}
works fine for me not the best solution I guess but it works :)
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