redirect

redirect

Signature

Description

Completes the request with a redirection response to a given targer URI and of a given redirection type (status code).

redirect is a convenience helper for completing the request with a redirection response. It is equivalent to this snippet relying on the complete method on RequestContext (a directive is also available):

Example

val route =
  pathPrefix("foo") {
    pathSingleSlash {
      complete("yes")
    } ~
      pathEnd {
        redirect("/foo/", StatusCodes.PermanentRedirect)
      }
  }

// tests:
Get("/foo/") ~> route ~> check {
  responseAs[String] shouldEqual "yes"
}

Get("/foo") ~> route ~> check {
  status shouldEqual StatusCodes.PermanentRedirect
  responseAs[String] shouldEqual """The request, and all future requests should be repeated using <a href="/foo/">this URI</a>."""
}

Contents