extract

Signature

def extract[T](f: RequestContext ⇒ T): Directive1[T]

Description

The extract directive is used as a building block for Custom Directives to extract data from the RequestContextRequestContext and provide it to the inner route. It is a special case for extracting one value of the more general textract directive that can be used to extract more than one value.

See Providing Values to Inner Routes for an overview of similar directives.

Example

Scala
val uriLength = extract(_.request.uri.toString.length)
val route =
  uriLength { len =>
    complete(s"The length of the request URI is $len")
  }

// tests:
Get("/abcdef") ~> route ~> check {
  responseAs[String] shouldEqual "The length of the request URI is 25"
}
Java
import static akka.http.javadsl.server.Directives.extract;

final Route route = extract(
  ctx -> ctx.getRequest().getUri().toString().length(),
  len -> complete("The length of the request URI is " + len)
);

// tests:
testRoute(route).run(HttpRequest.GET("/abcdef"))
  .assertEntity("The length of the request URI is 25");
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.