cookie

Description

Extracts a cookie with a given name from a request or otherwise rejects the request with a MissingCookieRejection if the cookie is missing.

Use the optionalCookie directive instead if you want to support missing cookies in your inner route.

Example

final Route route = cookie("userName", nameCookie ->
  complete("The logged in user is '" + nameCookie.value() + "'")
);

// tests:
testRoute(route).run(HttpRequest.GET("/").addHeader(Cookie.create("userName", "paul")))
  .assertEntity("The logged in user is 'paul'");
// missing cookie
runRouteUnSealed(route, HttpRequest.GET("/"))
  .assertRejections(Rejections.missingCookie("userName"));
testRoute(route).run(HttpRequest.GET("/"))
  .assertEntity("Request is missing required cookie 'userName'");
The source code for this page can be found here.