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
sourceval route =
  extractSettings { (settings: RoutingSettings) =>
    complete(s"RoutingSettings.renderVanityFooter = ${settings.renderVanityFooter}")
  }

// tests:
Get("/") ~> route ~> check {
  responseAs[String] shouldEqual "RoutingSettings.renderVanityFooter = true"
}
Java
sourceimport static org.apache.pekko.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");