extractRequest

Signature

def extractRequest: Directive1[HttpRequest]

Description

Extracts the complete HttpRequestHttpRequest 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
sourceval 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
sourceimport 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");
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.