extractRequest
Description
Extracts the complete HttpRequest
HttpRequest
instance.
Use extractRequest
to extract just the complete URI of the request. Usually there’s little use of extracting the complete request because extracting of most of the aspects of HttpRequests is handled by specialized directives. See Request Directives.
Example
- Scala
-
source
val route = extractRequest { request => complete(s"Request method is ${request.method.name} and content-type is ${request.entity.contentType}") } // tests: Post("/", "text") ~> route ~> check { responseAs[String] shouldEqual "Request method is POST and content-type is text/plain; charset=UTF-8" } Get("/") ~> route ~> check { responseAs[String] shouldEqual "Request method is GET and content-type is none/none" }
- Java
-
source
import static akka.http.javadsl.server.Directives.extractRequest; final Route route = extractRequest(request -> complete("Request method is " + request.method().name() + " and content-type is " + request.entity().getContentType()) ); // tests: testRoute(route).run(HttpRequest.POST("/").withEntity("text")) .assertEntity("Request method is POST and content-type is text/plain; charset=UTF-8"); testRoute(route).run(HttpRequest.GET("/")) .assertEntity("Request method is GET and content-type is none/none");