carson-katri / swift-request

Declarative HTTP networking, designed for SwiftUI
MIT License
727 stars 41 forks source link

Get IP address of Client that is submitting the request #35

Closed curveddesign closed 4 years ago

curveddesign commented 4 years ago

Is there a way to get the IP address of the Client machine or browser that is submitting the request? I am initialing a request from SwiftWebUI and I would like the IP address of the client machine.

    Request {
        Url("https://example.com/service")
        Method(.get)
        Header.Accept(.json)
    }
   .onJson { json in
        print(json)
    }
carson-katri commented 4 years ago

Request does not provide this functionality.

Although, it is possible to get the IP address of a device from Swift (see this StackOverflow answer for macOS). However, I don't believe what you are trying to do will work with this solution. The way SwiftWebUI works is that all of the Swift code you write runs on the server, and the website simply sends what element was triggered. So you would likely always get the IP address of the server returned.

If you want the IP address of the client when using SwiftWebUI, you probably need to create a custom Vapor server to run SwiftWebUI from, and use req.remotePeer.hostname to get the client IP address when the connection is opened.

Hope this helps.

curveddesign commented 4 years ago

Yes, I think I may have been going down the wrong path here!

This other StackOverflow answer works perfect with this swift-request library.

Request {
     Url("http://ip-api.com/json")
     Method(.get)
     Header.Accept(.json)
}

We can close this issue.