This documentation regards version 10.1, however the current version is 10.7.0.
extractMethod
Signature
def extractMethod:Directive1[HttpMethod]
Description
Extracts the HttpMethodHttpMethod from the request context and provides it for use for other directives explicitly.
Example
In the below example our route first matches all GET requests, and if an incoming request wasn’t a GET, the matching continues and the extractMethod route will be applied which we can use to programatically print what type of request it was - independent of what actual HttpMethod it was:
sourceval route =
concat(get{
complete("This is a GET request.")},
extractMethod { method =>
complete(s"This ${method.name} request, clearly is not a GET!")})// tests:Get("/")~> route ~> check {
responseAs[String] shouldEqual "This is a GET request."}Put("/")~> route ~> check {
responseAs[String] shouldEqual "This PUT request, clearly is not a GET!"}Head("/")~> route ~> check {
responseAs[String] shouldEqual "This HEAD request, clearly is not a GET!"}
sourceimport static akka.http.javadsl.server.Directives.complete;importstatic akka.http.javadsl.server.Directives.get;importstatic akka.http.javadsl.server.Directives.extractMethod;finalRoute route = concat(get(()->
complete("This is a GET request.")),
extractMethod(method ->
complete("This "+ method.value()+" request, clearly is not a GET!")));
testRoute(route).run(HttpRequest.GET("/")).assertEntity("This is a GET request.");
testRoute(route).run(HttpRequest.PUT("/").withEntity("put content")).assertEntity("This PUT request, clearly is not a GET!");
testRoute(route).run(HttpRequest.HEAD("/")).assertEntity("This HEAD request, clearly is not a GET!");
Custom Http Method
When you define a custom HttpMethod, you can define a route using extractMethod.