pathSuffix

Signature

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

Description

Matches and consumes a suffix 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 path matching 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 pathPrefix this directive matches and consumes the unmatched path from the right, i.e. the end.

Caution

For efficiency reasons, the given PathMatcher must match the desired suffix in reversed-segment order, i.e. pathSuffix("baz" / "bar") would match /foo/bar/baz! The order within a segment match is not reversed.

Depending on the type of its PathMatcher argument the pathPrefix 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("start") {
    concat(
      pathSuffix("end") {
        completeWithUnmatchedPath
      },
      pathSuffix("foo" / "bar" ~ "baz") {
        completeWithUnmatchedPath
      }
    )
  }

// tests:
Get("/start/middle/end") ~> route ~> check {
  responseAs[String] shouldEqual "/middle/"
}

Get("/start/something/barbaz/foo") ~> route ~> check {
  responseAs[String] shouldEqual "/something/"
}
Java
sourceimport static akka.http.javadsl.server.Directives.pathPrefix;
import static akka.http.javadsl.server.Directives.pathSuffix;
final Route route =
    concat(
        pathPrefix("start", () ->
            concat(
                pathSuffix("end", () -> completeWithUnmatchedPath.get()),
                pathSuffix(segment("foo").slash("bar").concat("baz"), () ->
                    completeWithUnmatchedPath.get())
            )
        )
    );
// tests:
testRoute(route).run(HttpRequest.GET("/start/middle/end")).assertEntity("/middle/");
testRoute(route).run(HttpRequest.GET("/start/something/barbaz/foo")).assertEntity("/something/");
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.