formFieldSeqformFieldList
Description
Extracts all HTTP form fields at once in the original order as (name, value) tuples of type (String, String)
Map.Entry<String, String>
. Data posted from HTML Forms is either of type application/x-www-form-urlencoded
or of type multipart/form-data
.
This directive can be used if the exact order of form fields is important or if parameters can occur several times.
See formFields for an in-depth description.
Warning
The directive reads all incoming HTTP form fields without any configured upper bound. It means, that requests with form fields holding significant amount of data (ie. during a file upload) can cause performance issues or even an OutOfMemoryError
s.
Example
- Scala
-
source
val route = formFieldSeq { fields => def formFieldString(formField: (String, String)): String = s"""${formField._1} = '${formField._2}'""" complete(s"The form fields are ${fields.map(formFieldString).mkString(", ")}") } // tests: Post("/", FormData("color" -> "blue", "count" -> "42")) ~> route ~> check { responseAs[String] shouldEqual "The form fields are color = 'blue', count = '42'" } Post("/", FormData("x" -> "23", "x" -> "4", "x" -> "89")) ~> route ~> check { responseAs[String] shouldEqual "The form fields are x = '23', x = '4', x = '89'" }
- Java
-
source
import static akka.http.javadsl.server.Directives.complete; import static akka.http.javadsl.server.Directives.formFieldList; final Function<List<Entry<String, String>>, String> listToString = list -> list.stream() .map(e -> e.getKey() + " = '" + e.getValue() +"'") .collect(Collectors.joining(", ")); final Route route = formFieldList(fields -> complete("The form fields are " + listToString.apply(fields)) ); // tests: final FormData formDataDiffKey = FormData.create( Pair.create("color", "blue"), Pair.create("count", "42")); testRoute(route).run(HttpRequest.POST("/").withEntity(formDataDiffKey.toEntity())) .assertEntity("The form fields are color = 'blue', count = '42'"); final FormData formDataSameKey = FormData.create( Pair.create("x", "23"), Pair.create("x", "4"), Pair.create("x", "89")); testRoute(route).run(HttpRequest.POST("/").withEntity(formDataSameKey.toEntity())) .assertEntity("The form fields are x = '23', x = '4', x = '89'");