Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
layout title date categories post_photo
post
Using an HTTP proxy on Play Framework's WSClient
2021-01-10 20:35:32 -0700
scala
assets/posts/using-http-proxy/post_photo.jpg

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.