Proxy
note
This help topic is in development and will be updated in the future.
Ktor HTTP client allows using proxy in multiplatform code. The following document describes how to configure a proxy in ktor.
Multiplatform configuration
Create proxy
You don’t need to include additional artifacts to create a proxy. The supported proxy types are specific to a client engine. Two types of proxy can be configured in multiplatform: HTTP and SOCKS.
To create a proxy configuration use builders in the ProxyBuilder factory:
// Create http proxy
val httpProxy = ProxyBuilder.http("http://my-proxy-server-url.com/")
// Create socks proxy
val socksProxy = ProxyBuilder.socks(host = "127.0.0.1", port = 4001)
Proxy authentication and authorization are engine specific and should be handled by the user manually.
Set proxy
Proxy can be configured in multiplatform code using ProxyConfig builder in HttpClientEngineConfig block:
val client = HttpClient() {
engine {
proxy = httpProxy
}
}
Platform-specific configuration
Jvm
The ProxyConfig class maps to Proxy class on the jvm:
val httpProxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(4040))
The most of Jvm client engines support it out of the box.
Note: Apache and CIO engines support HTTP proxy only. Jetty client engine doesn’t support any proxy.
Native
The native ProxyConfig class can use url to determine proxy address:
val socksProxy = ProxyConfig(url = "socks://my-socks-proxy.com/")
Supported proxy types are engine specific. To see supported URLs consult with engine provider documentation:
Js
The proxy configuration is unsupported by platform restrictions.