complete
Signature
def complete[T :ToResponseMarshaller](value: T): StandardRoute
def complete(response: HttpResponse): StandardRoute
def complete(status: StatusCode): StandardRoute
def complete[T :Marshaller](status: StatusCode, value: T): StandardRoute
def complete[T :Marshaller](status: Int, value: T): StandardRoute
def complete[T :Marshaller](status: StatusCode, headers: Seq[HttpHeader], value: T): StandardRoute
def complete[T :Marshaller](status: Int, headers: Seq[HttpHeader], value: T): StandardRoute
The signature shown is simplified, the real signature uses magnets. [1]
[1] | See The Magnet Pattern for an explanation of magnet-based overloading. |
Description
Completes the request using the given argument(s).
complete
uses the given arguments to construct a Route
which simply calls complete
on the RequestContext
with the respective HttpResponse
instance.
Completing the request will send the response "back up" the route structure where all the logic runs that wrapping
directives have potentially chained into the ルートリザルト future transformation chain.
Example
val route =
path("a") {
complete(HttpResponse(entity = "foo"))
} ~
path("b") {
complete(StatusCodes.OK)
} ~
path("c") {
complete(StatusCodes.Created -> "bar")
} ~
path("d") {
complete(201 -> "bar")
} ~
path("e") {
complete(StatusCodes.Created, List(`Content-Type`(`text/plain(UTF-8)`)), "bar")
} ~
path("f") {
complete(201, List(`Content-Type`(`text/plain(UTF-8)`)), "bar")
} ~
(path("g") & complete("baz")) // `&` also works with `complete` as the 2nd argument
// tests:
Get("/a") ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "foo"
}
Get("/b") ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "OK"
}
Get("/c") ~> route ~> check {
status shouldEqual StatusCodes.Created
responseAs[String] shouldEqual "bar"
}
Get("/d") ~> route ~> check {
status shouldEqual StatusCodes.Created
responseAs[String] shouldEqual "bar"
}
Get("/e") ~> route ~> check {
status shouldEqual StatusCodes.Created
header[`Content-Type`] shouldEqual Some(`Content-Type`(`text/plain(UTF-8)`))
responseAs[String] shouldEqual "bar"
}
Get("/f") ~> route ~> check {
status shouldEqual StatusCodes.Created
header[`Content-Type`] shouldEqual Some(`Content-Type`(`text/plain(UTF-8)`))
responseAs[String] shouldEqual "bar"
}
Get("/g") ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "baz"
}
Contents