'How to get client IP address in Vapor 3.0?
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?
Solution 1:[1]
Thanks to the Vapor community, I have got an answer.
You can use req.http.remotePeer.hostname
for Vapor 3.0 project.
Solution 2:[2]
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 :)
Solution 3:[3]
For Vapor 4 use this
req.remoteAddress?.ipAddress
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 5t111111 |
Solution 2 | Robin Kment |
Solution 3 | 3366784 |