Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 1.19 KB

2021-01-10-using-an-http-proxy-on-playframework-wsclient.md

File metadata and controls

29 lines (23 loc) · 1.19 KB
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.