mapRequest

Transforms the request before it is handled by the inner route.

Signature

def mapRequest(f: HttpRequest  HttpRequest): Directive0 

Description

The mapRequest directive is used as a building block for Custom Directives to transform a request before it is handled by the inner route. Changing the request.uri parameter has no effect on path matching in the inner route because the unmatched path is a separate field of the RequestContext value which is passed into routes. To change the unmatched path or other fields of the RequestContext use the mapRequestContext directive.

See Directives transforming the request for an overview of similar directives.

Example

def transformToPostRequest(req: HttpRequest): HttpRequest = req.copy(method = HttpMethods.POST)
val route =
  mapRequest(transformToPostRequest) {
    requestInstance { req =>
      complete(s"The request method was ${req.method}")
    }
  }

Get("/") ~> route ~> check {
  responseAs[String] === "The request method was POST"
}