Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get host from a client? #837

Open
sydt2014 opened this issue Mar 15, 2020 · 1 comment
Open

how to get host from a client? #837

sydt2014 opened this issue Mar 15, 2020 · 1 comment

Comments

@sydt2014
Copy link

one client like this :
val client: Service[Request, Response] = Http.newService("st1-druid-3:8090"),
and use
client(request),
but how to set request.host = "st1-druid-3" from client ?

@ryanoneill
Copy link
Contributor

Hi @sydt2014,

You have two options:

  1. Set the host header directly on the request for each request before passing it to your client.
  2. Add a filter to your client which sets the host header automatically.
import com.twitter.conversions.DurationOps._
import com.twitter.finagle.{Http, Service, SimpleFilter}
import com.twitter.util.Future

val client = Http.client.newService("www.google.com:80")
val request1 = Request("/")
request1.host = "www.google.com"
val response = Await.result(client(request1), 1.second)

// or
val hostFilter = new SimpleFilter[Request, Response] {
  def apply(request: Request, service: Service[Request, Response]): Future[Response] = {
    request.host = "www.google.com"
    service(request)
  }
}
val filteredClient = hostFilter.andThen(service)
val request2 = Request("/")

Await.result(client(request2), 1.second)
request2.host
// ... Option[String] = None

Await.result(filteredClient(request2), 1.second)
request2.host
// ... Option[String] = Some(www.google.com)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants