extractRequestContext

extractRequestContext

§Signature

§Description

Extracts the request's underlying RequestContext.

This directive is used as a building block for most of the other directives, which extract the context and by inspecting some of it's values can decide what to do with the request - for example provide a value, or reject the request.

See also extractRequest if only interested in the HttpRequest instance itself.

§Example

  1. val route =
  2. extractRequestContext { ctx =>
  3. ctx.log.debug("Using access to additional context availablethings, like the logger.")
  4. val request = ctx.request
  5. complete(s"Request method is ${request.method.name} and content-type is ${request.entity.contentType}")
  6. }
  7.  
  8. // tests:
  9. Post("/", "text") ~> route ~> check {
  10. responseAs[String] shouldEqual "Request method is POST and content-type is text/plain; charset=UTF-8"
  11. }
  12. Get("/") ~> route ~> check {
  13. responseAs[String] shouldEqual "Request method is GET and content-type is none/none"
  14. }