anyParams
The anyParams directive allows to extract values both from query parameters and form fields.
Signature
def anyParams(params: <ParamDef[T_i]>*): Directive[T_0 :: ... T_i ... :: HNil]
def anyParams(params: <ParamDef[T_0]> :: ... <ParamDef[T_i]> ... :: HNil): Directive[T_0 :: ... T_i ... :: HNil]
The signature shown is simplified and written in pseudo-syntax, the real signature uses magnets. [1] The type <ParamDef> doesn’t really exist but consists of the syntactic variants as shown in the description and the examples of the parameters directive.
[1] | See The Magnet Pattern for an explanation of magnet-based overloading. |
Description
The directives combines the functionality from parameters and formFields in one directive. To be able to unmarshal a parameter to a value of a specific type (e.g. with as[Int]) you need to fulfill the requirements as explained both for parameters and formFields.
There’s a singular version, anyParam.
Example
val route =
anyParams('name, 'age.as[Int])((name, age) =>
complete(s"$name is $age years old")
)
// extracts query parameters
Get("/?name=Herman&age=168") ~> route ~> check {
responseAs[String] === "Herman is 168 years old"
}
// extracts form fields
Post("/", FormData(Seq("name" -> "Herman", "age" -> "168"))) ~> route ~> check {
responseAs[String] === "Herman is 168 years old"
}