complete

Completes the request using the given argument(s).

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

complete uses the given arguments to construct a Route which simply calls requestContext.complete with the respective HttpResponse instance. Completing the request will send the response “back up” the route structure where all logic that wrapping directives have potentially chained into the responder chain is run (see also The Responder Chain). Once the response hits the top-level runRoute logic it is sent back to the underlying spray-can or spray-servlet layer which will trigger the sending of the actual HTTP response message back to the client.

Example

val route =
  path("a") {
    complete(HttpResponse(entity = "foo"))
  } ~
  path("b") {
    complete(StatusCodes.Created, "bar")
  } ~
  (path("c") & complete("baz")) // `&` also works with `complete` as the 2nd argument

Get("/a") ~> route ~> check {
  status === StatusCodes.OK
  responseAs[String] === "foo"
}

Get("/b") ~> route ~> check {
  status === StatusCodes.Created
  responseAs[String] === "bar"
}

Get("/c") ~> route ~> check {
  status === StatusCodes.OK
  responseAs[String] === "baz"
}