extractSettings

Signature

def extractSettings: Directive1[RoutingSettings]

Description

Extracts the RoutingSettingsRoutingSettings from the RequestContextRequestContext.

By default the settings of the Http() extension running the route will be returned. It is possible to override the settings for specific sub-routes by using the withSettings directive.

Example

Scala
val route =
  extractSettings { settings: RoutingSettings =>
    complete(s"RoutingSettings.renderVanityFooter = ${settings.renderVanityFooter}")
  }

// tests:
Get("/") ~> route ~> check {
  responseAs[String] shouldEqual "RoutingSettings.renderVanityFooter = true"
}
Java
import static akka.http.javadsl.server.Directives.extractRequestContext;

final Route route = extractRequestContext(ctx -> {
  ctx.getLog().debug("Using access to additional context available, like the logger.");
  final HttpRequest request = ctx.getRequest();
  return 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.