Menu spray
Search docs NEW: Watch an intro talk on spray!
  • Home
  • Introduction
    • What is spray?
    • Getting Started
    • spray for Web Development
    • Other Resources
  • Documentation
    • 1.2.4 / 1.3.4
    • 1.1.4
    • API (snapshot)
  • Project Info
    • Current Versions
    • Migration from M8
    • Maven Repository
    • Contributing
    • Changelog
    • Credits
    • References
    • Sponsors
    • License
    • Community Projects
  • Blog
    • spray on the Raspberry Pi
    • spray 1.0-M8 / 1.1-M8 / 1.2-M8 released
    • Benchmarking spray
    • The Magnet Pattern
    • Welcome to the spray Blog
  • Contact
    • Mailing List
    • Twitter
    • Email
    • Commercial Support
  • Home /
  • Documentation /
  • 1.2.4 / 1.3.4 /
  • spray-routing /
  • Predefined Directives (by trait) /
  • List of predefined directives by trait /
  • CookieDirectives /
  • cookie
  • CookieDirectives
  • cookie
    • Signature
    • Description
    • Example
  • deleteCookie
  • optionalCookie
  • setCookie

cookie

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

Signature

def cookie(name: String): Directive1[HttpCookie] 

Description

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

Example

val route =
  cookie("userName") { nameCookie =>
    complete(s"The logged in user is '${nameCookie.content}'")
  }


Get("/") ~> Cookie(HttpCookie("userName", "paul")) ~> route ~> check {
  responseAs[String] === "The logged in user is 'paul'"
}
// missing cookie
Get("/") ~> route ~> check {
  rejection === MissingCookieRejection("userName")
}
Get("/") ~> sealRoute(route) ~> check {
  responseAs[String] === "Request is missing required cookie 'userName'"
}
© 2014 Typesafe, Inc.
Home| Introduction| Documentation| Project Info| Blog| Contact| Index
powered by spray

spray is no longer maintained and has been superseded by Akka HTTP.
Please check out the migration guide for help with the upgrade.
Commercial support is available from Lightbend.