finagle
finagle copied to clipboard
how to get host from a client?
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 ?
Hi @sydt2014,
You have two options:
- Set the host header directly on the request for each request before passing it to your client.
- 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)