headerValueByName

Extracts the value of the HTTP request header with the given name.

Signature

def headerValueByName(headerName: Symbol): Directive1[String] 
def headerValueByName(headerName: String): Directive1[String] 

Description

The name can be given as a String or as a Symbol. If no header with a matching name is found the request is rejected with a MissingHeaderRejection. If the header is expected to be missing in some cases or to customize handling when the header is missing use the optionalHeaderValueByName directive instead.

Example

val route =
  headerValueByName("X-User-Id") { userId =>
    complete(s"The user is $userId")
  }

Get("/") ~> RawHeader("X-User-Id", "Joe42") ~> route ~> check {
  responseAs[String] === "The user is Joe42"
}

Get("/") ~> sealRoute(route) ~> check {
  status === BadRequest
  responseAs[String] === "Request is missing required HTTP header 'X-User-Id'"
}