deleteCookie

Signature

def deleteCookie(first: HttpCookie, more: HttpCookie*): Directive0
def deleteCookie(name: String, domain: String = "", path: String = ""): Directive0

Description

Adds a header to the response to request the removal of the cookie with the given name on the client.

Use the setCookie directive to update a cookie.

Example

Scala
sourceval route =
  deleteCookie("userName") {
    complete("The user was logged out")
  }

// tests:
Get("/") ~> route ~> check {
  responseAs[String] shouldEqual "The user was logged out"
  header[`Set-Cookie`] shouldEqual Some(`Set-Cookie`(HttpCookie("userName", value = "deleted", expires = Some(DateTime.MinValue))))
}
Java
sourceimport static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.deleteCookie;

final Route route = deleteCookie("userName", () ->
  complete("The user was logged out")
);

// tests:
final HttpHeader expected = SetCookie.create(
  HttpCookie.create(
    "userName",
    "deleted",
    Optional.of(DateTime.MinValue()),
    OptionalLong.empty(),
    Optional.empty(),
    Optional.empty(),
    false,
    false,
    Optional.empty(),
    Optional.empty()));

testRoute(route).run(HttpRequest.GET("/"))
  .assertEntity("The user was logged out")
  .assertHeaderExists(expected);
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.