headerValuePF

Calls the specified partial function with the first request header the function is isDefinedAt and extracts the result of calling the function.

Signature

def headerValuePF[T](pf: PartialFunction[HttpHeader, T]): Directive1[T] 

Description

The headerValuePF directive is an alternative syntax version of headerValue. If the function throws an exception the request is rejected with a MalformedHeaderRejection. If the function is not defined for any header the request is rejected as “NotFound”.

Example

def extractHostPort: PartialFunction[HttpHeader, Int] = {
  case h: `Host`=> h.port
}

val route =
  headerValuePF(extractHostPort) { port =>
    complete(s"The port was $port")
  }

Get("/") ~> Host("example.com", 5043) ~> route ~> check {
  responseAs[String] === "The port was 5043"
}
Get("/") ~> sealRoute(route) ~> check {
  status === NotFound
  responseAs[String] === "The requested resource could not be found."
}