scheme

Rejects a request if its Uri scheme does not match a given one.

Signature

def scheme(schm: String): Directive0 

Description

The scheme directive can be used to match requests by their Uri scheme, only passing through requests that match the specified scheme and rejecting all others.

A typical use case for the scheme directive would be to reject requests coming in over http instead of https, or to redirect such requests to the matching https URI with a MovedPermanently.

For simply extracting the scheme name, see the schemeName directive.

Example

val route =
  scheme("http") {
    extract(_.request.uri) { uri 
      redirect(uri.copy(scheme = "https"), MovedPermanently)
    }
  } ~
  scheme("https") {
    complete(s"Safe and secure!")
  }

Get("http://www.example.com/hello") ~> route ~> check {
  status === MovedPermanently
  header[Location] === Some(Location(Uri("https://www.example.com/hello")))
}

Get("https://www.example.com/hello") ~> route ~> check {
  responseAs[String] === "Safe and secure!"
}