rawPathPrefix

Signature

def rawPathPrefix[L](pm: PathMatcher[L]): Directive[L]

Description

Matches and consumes a prefix of the unmatched path of the RequestContextRequestContext against the given PathMatcher, potentially extracts one or more values (depending on the type of the argument).

This directive filters incoming requests based on the part of their URI that hasn’t been matched yet by other potentially existing rawPathPrefix or pathPrefix directives on higher levels of the routing structure. Its one parameter is usually an expression evaluating to a PathMatcher instance (see also: The PathMatcher DSL).

As opposed to its pathPrefix counterpart rawPathPrefix does not automatically add a leading slash to its PathMatcher argument. Rather its PathMatcher argument is applied to the unmatched path as is. For a comparison between path directives check Overview of path directives.

Depending on the type of its PathMatcher argument the rawPathPrefix directive extracts zero or more values from the URI. If the match fails the request is rejected with an empty rejection set.

Example

Scala
sourceval completeWithUnmatchedPath =
  extractUnmatchedPath { p =>
    complete(p.toString)
  }

val route =
  pathPrefix("foo") {
    concat(
      rawPathPrefix("bar") { completeWithUnmatchedPath },
      rawPathPrefix("doo") { completeWithUnmatchedPath }
    )
  }

// tests:
Get("/foobar/baz") ~> route ~> check {
  responseAs[String] shouldEqual "/baz"
}

Get("/foodoo/baz") ~> route ~> check {
  responseAs[String] shouldEqual "/baz"
}
Java
sourceimport static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.rawPathPrefixTest;
final Route route =
    concat(
        pathPrefix("foo", () ->
            rawPathPrefixTest("bar", () -> completeWithUnmatchedPath.get())
        )
    );
// tests:
testRoute(route).run(HttpRequest.GET("/foobar")).assertEntity("bar");
testRoute(route).run(HttpRequest.GET("/foobaz")).assertStatusCode(StatusCodes.NOT_FOUND);
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.