optionalCookie

Signature

def optionalCookie(name: String): Directive1[Option[HttpCookiePair]]

Description

Extracts an optional cookie with a given name from a request.

Use the cookie directive instead if the inner route does not handle a missing cookie.

Example

val route =
  optionalCookie("userName") {
    case Some(nameCookie) => complete(s"The logged in user is '${nameCookie.value}'")
    case None             => complete("No user logged in")
  }

// tests:
Get("/") ~> Cookie("userName" -> "paul") ~> route ~> check {
  responseAs[String] shouldEqual "The logged in user is 'paul'"
}
Get("/") ~> route ~> check {
  responseAs[String] shouldEqual "No user logged in"
}
The source code for this page can be found here.