extractHost

Extract the hostname part of the Host request header and expose it as a String extraction to its inner route.

Signature

def extractHost: Directive1[String]

Example

Scala
sourceval route =
  extractHost { hn =>
    complete(s"Hostname: $hn")
  }

// tests:
Get() ~> Host("company.com", 9090) ~> route ~> check {
  status shouldEqual OK
  responseAs[String] shouldEqual "Hostname: company.com"
}
Java
sourceimport static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.extractHost;


final Route route = extractHost(hn -> 
    complete("Hostname: " + hn));

testRoute(route).run(HttpRequest.GET("/").addHeader(Host.create("company.com", 9090)))
    .assertEntity("Hostname: company.com");
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.