complete

complete

§Signature

  1. def complete[T :ToResponseMarshaller](value: T): StandardRoute
  2. def complete(response: HttpResponse): StandardRoute
  3. def complete(status: StatusCode): StandardRoute
  4. def complete[T :Marshaller](status: StatusCode, value: T): StandardRoute
  5. def complete[T :Marshaller](status: Int, value: T): StandardRoute
  6. def complete[T :Marshaller](status: StatusCode, headers: Seq[HttpHeader], value: T): StandardRoute
  7. 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 RouteResult future transformation chain.

§Example

  1. val route =
  2. path("a") {
  3. complete(HttpResponse(entity = "foo"))
  4. } ~
  5. path("b") {
  6. complete(StatusCodes.OK)
  7. } ~
  8. path("c") {
  9. complete(StatusCodes.Created -> "bar")
  10. } ~
  11. path("d") {
  12. complete(201 -> "bar")
  13. } ~
  14. path("e") {
  15. complete(StatusCodes.Created, List(`Content-Type`(`text/plain(UTF-8)`)), "bar")
  16. } ~
  17. path("f") {
  18. complete(201, List(`Content-Type`(`text/plain(UTF-8)`)), "bar")
  19. } ~
  20. (path("g") & complete("baz")) // `&` also works with `complete` as the 2nd argument
  21.  
  22. // tests:
  23. Get("/a") ~> route ~> check {
  24. status shouldEqual StatusCodes.OK
  25. responseAs[String] shouldEqual "foo"
  26. }
  27.  
  28. Get("/b") ~> route ~> check {
  29. status shouldEqual StatusCodes.OK
  30. responseAs[String] shouldEqual "OK"
  31. }
  32.  
  33. Get("/c") ~> route ~> check {
  34. status shouldEqual StatusCodes.Created
  35. responseAs[String] shouldEqual "bar"
  36. }
  37.  
  38. Get("/d") ~> route ~> check {
  39. status shouldEqual StatusCodes.Created
  40. responseAs[String] shouldEqual "bar"
  41. }
  42.  
  43. Get("/e") ~> route ~> check {
  44. status shouldEqual StatusCodes.Created
  45. header[`Content-Type`] shouldEqual Some(`Content-Type`(`text/plain(UTF-8)`))
  46. responseAs[String] shouldEqual "bar"
  47. }
  48.  
  49. Get("/f") ~> route ~> check {
  50. status shouldEqual StatusCodes.Created
  51. header[`Content-Type`] shouldEqual Some(`Content-Type`(`text/plain(UTF-8)`))
  52. responseAs[String] shouldEqual "bar"
  53. }
  54.  
  55. Get("/g") ~> route ~> check {
  56. status shouldEqual StatusCodes.OK
  57. responseAs[String] shouldEqual "baz"
  58. }