optionalCookie

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

final Route route = optionalCookie("userName", optNameCookie -> {
  if (optNameCookie.isPresent()) {
    return complete("The logged in user is '" + optNameCookie.get().value() + "'");
  } else {
    return complete("No user logged in");
  }
}
);

// tests:
testRoute(route).run(HttpRequest.GET("/").addHeader(Cookie.create("userName", "paul")))
  .assertEntity("The logged in user is 'paul'");
testRoute(route).run(HttpRequest.GET("/"))
  .assertEntity("No user logged in");
The source code for this page can be found here.