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:

Scala
val 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!"
}
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.get;
import static akka.http.javadsl.server.Directives.extractMethod;


final Route 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.

Scala
import akka.http.scaladsl.settings.{ ParserSettings, ServerSettings }

// define custom method type:
val BOLT = HttpMethod.custom("BOLT", safe = false,
  idempotent = true, requestEntityAcceptance = Expected)

// add custom method to parser settings:
val parserSettings = ParserSettings(system).withCustomMethods(BOLT)
val serverSettings = ServerSettings(system).withParserSettings(parserSettings)

val routes = extractMethod { method =>
  complete(s"This is a ${method.name} method request.")
}
val binding = Http().bindAndHandle(routes, host, port, settings = serverSettings)

val request = HttpRequest(BOLT, s"http://$host:$port/", protocol = `HTTP/1.1`)
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.route;
import static akka.http.javadsl.server.Directives.extractMethod;


// define custom method type:
HttpMethod BOLT =
  HttpMethods.custom("BOLT", false, true, Expected);

// add custom method to parser settings:
final ParserSettings parserSettings =
  ParserSettings.create(system).withCustomMethods(BOLT);
final ServerSettings serverSettings =
  ServerSettings.create(system).withParserSettings(parserSettings);

final Route routes = concat(
  extractMethod( method ->
    complete( "This is a " + method.name() + " request.")
  )
);
final Flow<HttpRequest, HttpResponse, NotUsed> handler = routes.flow(system, materializer);
final Http http = Http.get(system);
final CompletionStage<ServerBinding> binding =
  http.bindAndHandle(
    handler,
    ConnectHttp.toHost(host, port),
    serverSettings,
    loggingAdapter,
    materializer);

HttpRequest request = HttpRequest.create()
  .withUri("http://" + host + ":" + Integer.toString(port))
  .withMethod(BOLT)
  .withProtocol(HTTP_1_1);

CompletionStage<HttpResponse> response = http.singleRequest(request, materializer);
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.