extractMatchedPath

Signature

def extractMatchedPath: Directive1[Uri.Path]

Description

Extracts the matched path from the request context.

The extractMatchedPath directive extracts the path that was already matched by any of the PathDirectives (or any custom ones that change the unmatched path field of the request context). You can use it for building directives that use already matched part in their logic.

See also extractUnmatchedPath to see similar directive for unmatched path.

Example

Scala
sourceval route =
  pathPrefix("abc") {
    extractMatchedPath { matched =>
      complete(matched.toString)
    }
  }

// tests:
Get("/abc") ~> route ~> check {
  responseAs[String] shouldEqual "/abc"
}
Get("/abc/xyz") ~> route ~> check {
  responseAs[String] shouldEqual "/abc"
}
Java
sourceimport static akka.http.javadsl.server.Directives.extractMatchedPath;

final Route route = pathPrefix("abc", () -> extractMatchedPath(Directives::complete));

// tests:
testRoute(route).run(HttpRequest.GET("/abc")).assertEntity("/abc");
testRoute(route).run(HttpRequest.GET("/abc/xyz")).assertEntity("/abc");
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.