Using an HTTP proxy on Play Framework's WSClient
If you ever had the need to use http proxies from your JVM-based apps, you likely faced the pain involved with the Java Authenticator. Gladly, Play Framework’s WSClient has a cleaner way/
Instead of polluting the global scope with custom authenticators, you just need to create an instance of play.api.libs.ws.WSProxyServer
, which can be set to every request while using the WSClient
, like this:
import play.api.libs.ws.{DefaultWSProxyServer, WSClient}
val proxy = DefaultWSProxyServer(
host = "myserver.com", // no protocol on purpose
port = 8000,
principal = Some(username), // needed if basic-authentication is required
password = Some(password), // needed if basic-authentication is required
protocol = Some("https")
)
def f(ws: WSClient) = {
ws.url("https://wiringbits.net").withProxyServer(proxy)
}
That’s it, far simpler than dealing with the Java way.
The source of this post can be found here