Skip to content

Instantly share code, notes, and snippets.

@MihaelIsaev
Created June 26, 2018 02:49
Show Gist options
  • Save MihaelIsaev/83866f306be7ebbc8201b12e1b452029 to your computer and use it in GitHub Desktop.
Save MihaelIsaev/83866f306be7ebbc8201b12e1b452029 to your computer and use it in GitHub Desktop.
Example of http request through proxy for Vapor 3
public func boot(_ app: Application) throws {
let config = URLSessionConfiguration.default
config.requestCachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
config.connectionProxyDictionary = [AnyHashable: Any]()
config.connectionProxyDictionary?[kCFNetworkProxiesHTTPEnable as String] = 1
config.connectionProxyDictionary?[kCFNetworkProxiesHTTPProxy as String] = "proxy-server.com"
config.connectionProxyDictionary?[kCFNetworkProxiesHTTPPort as String] = 8080
let session = URLSession.init(configuration: config)
let client = FoundationClient(session, on: app)
let httpReq: HTTPRequest = HTTPRequest(method: HTTPMethod.GET, url: "http://destinationsite.com")
let req = Request(http: httpReq, using: app)
_ = client.send(req).map { res in
debugPrint(res)
}.catchMap { err in
debugPrint(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment