mapRequestContext

Signature

def mapRequestContext(f: RequestContext ⇒ RequestContext): Directive0

Description

Transforms the RequestContextRequestContext before it is passed to the inner route.

The mapRequestContext directive is used as a building block for Custom Directives to transform the request context before it is passed to the inner route. To change only the request value itself the mapRequest directive can be used instead.

See Request Transforming Directives for an overview of similar directives.

Example

Scala
val replaceRequest =
  mapRequestContext(_.withRequest(HttpRequest(HttpMethods.POST)))

val route =
  replaceRequest {
    extractRequest { req =>
      complete(req.method.value)
    }
  }

// tests:
Get("/abc/def/ghi") ~> route ~> check {
  responseAs[String] shouldEqual "POST"
}
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.extractRequest;
import static akka.http.javadsl.server.Directives.mapRequestContext;

final Route route = mapRequestContext(ctx ->
  ctx.withRequest(HttpRequest.create().withMethod(HttpMethods.POST)), () ->
  extractRequest(req -> complete(req.method().value()))
);

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