formField

formField

§Signature

§Description

Allows extracting a single Form field sent in the request.

See formFields for an in-depth description.

§Example

  1. val route =
  2. formFields('color, 'age.as[Int]) { (color, age) =>
  3. complete(s"The color is '$color' and the age ten years ago was ${age - 10}")
  4. }
  5.  
  6. // tests:
  7. Post("/", FormData("color" -> "blue", "age" -> "68")) ~> route ~> check {
  8. responseAs[String] shouldEqual "The color is 'blue' and the age ten years ago was 58"
  9. }
  10.  
  11. Get("/") ~> Route.seal(route) ~> check {
  12. status shouldEqual StatusCodes.BadRequest
  13. responseAs[String] shouldEqual "Request is missing required form field 'color'"
  14. }
  15. val route =
  16. formField('color) { color =>
  17. complete(s"The color is '$color'")
  18. } ~
  19. formField('id.as[Int]) { id =>
  20. complete(s"The id is '$id'")
  21. }
  22.  
  23. // tests:
  24. Post("/", FormData("color" -> "blue")) ~> route ~> check {
  25. responseAs[String] shouldEqual "The color is 'blue'"
  26. }
  27.  
  28. Get("/") ~> Route.seal(route) ~> check {
  29. status shouldEqual StatusCodes.BadRequest
  30. responseAs[String] shouldEqual "Request is missing required form field 'color'"
  31. }
  32. val route =
  33. formFieldMap { fields =>
  34. def formFieldString(formField: (String, String)): String =
  35. s"""${formField._1} = '${formField._2}'"""
  36. complete(s"The form fields are ${fields.map(formFieldString).mkString(", ")}")
  37. }
  38.  
  39. // tests:
  40. Post("/", FormData("color" -> "blue", "count" -> "42")) ~> route ~> check {
  41. responseAs[String] shouldEqual "The form fields are color = 'blue', count = '42'"
  42. }
  43. Post("/", FormData("x" -> "1", "x" -> "5")) ~> route ~> check {
  44. responseAs[String] shouldEqual "The form fields are x = '5'"
  45. }
  46. val route =
  47. formFieldMultiMap { fields =>
  48. complete("There are " +
  49. s"form fields ${fields.map(x => x._1 + " -> " + x._2.size).mkString(", ")}")
  50. }
  51.  
  52. // tests:
  53. Post("/", FormData("color" -> "blue", "count" -> "42")) ~> route ~> check {
  54. responseAs[String] shouldEqual "There are form fields color -> 1, count -> 1"
  55. }
  56. Post("/", FormData("x" -> "23", "x" -> "4", "x" -> "89")) ~> route ~> check {
  57. responseAs[String] shouldEqual "There are form fields x -> 3"
  58. }
  59. val route =
  60. formFieldSeq { fields =>
  61. def formFieldString(formField: (String, String)): String =
  62. s"""${formField._1} = '${formField._2}'"""
  63. complete(s"The form fields are ${fields.map(formFieldString).mkString(", ")}")
  64. }
  65.  
  66. // tests:
  67. Post("/", FormData("color" -> "blue", "count" -> "42")) ~> route ~> check {
  68. responseAs[String] shouldEqual "The form fields are color = 'blue', count = '42'"
  69. }
  70. Post("/", FormData("x" -> "23", "x" -> "4", "x" -> "89")) ~> route ~> check {
  71. responseAs[String] shouldEqual "The form fields are x = '23', x = '4', x = '89'"
  72. }