finagle icon indicating copy to clipboard operation
finagle copied to clipboard

how to get host from a client?

Open sydt2014 opened this issue 4 years ago • 1 comments

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 ?

sydt2014 avatar Mar 15 '20 12:03 sydt2014

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)

ryanoneill avatar Mar 16 '20 17:03 ryanoneill