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 RouteRouteRoute which simply calls complete on the RequestContextRequestContext with the respective HttpResponseHttpResponse 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 RouteResult future transformation chain.

Please note that the complete directive has multiple variants, like the ones shown in the examples.

Example

Scala
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.ContentTypes._
import akka.http.scaladsl.model.headers.`Content-Type`

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(Future { StatusCodes.Created -> "bar" })
    } ~
    (path("h") & 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.Created
  responseAs[String] shouldEqual "bar"
}

Get("/h") ~> route ~> check {
  status shouldEqual StatusCodes.OK
  responseAs[String] shouldEqual "baz"
}
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.path;

final Route route = concat(
  path("a", () -> complete(HttpResponse.create().withEntity("foo"))),
  path("b", () -> complete(StatusCodes.OK)),
  path("c", () -> complete(StatusCodes.CREATED, "bar")),
  path("d", () -> complete(StatusCodes.get(201), "bar")),
  path("e", () ->
    complete(StatusCodes.CREATED,
             Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)),
             HttpEntities.create("bar"))),
  path("f", () ->
    complete(StatusCodes.get(201),
             Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)),
             HttpEntities.create("bar"))),
  path("g", () -> complete("baz"))
);

// tests:
testRoute(route).run(HttpRequest.GET("/a"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("foo");

testRoute(route).run(HttpRequest.GET("/b"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("OK");

testRoute(route).run(HttpRequest.GET("/c"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/d"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/e"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8))
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/f"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8))
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/g"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("baz");
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.