extractRequestContext

extractRequestContext

§シグネチャ

§説明

リクエストの根底にある:class:`RequestContext`を抽出します。

このディレクティブは、他のディレクティブのビルディングブロックとして使用され、コンテキストを抽出し、その値の一部を調べることによって、リクエストを処理するかどうかを決定できます。たとえば、値を指定するか、リクエストを拒否します。

HttpRequest`インスタンス自体にのみ興味がある場合は:ref:-extractRequest-`を参照してください。

§

  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. }