Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client IP address in Vapor 3.0?

Tags:

swift

vapor

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?

like image 384
5t111111 Avatar asked May 24 '18 06:05

5t111111


2 Answers

Thanks to the Vapor community, I have got an answer.

You can use req.http.remotePeer.hostname for Vapor 3.0 project.

like image 175
5t111111 Avatar answered Oct 02 '22 03:10

5t111111


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 :)

like image 29
Robin Kment Avatar answered Oct 02 '22 03:10

Robin Kment