parameter

Signature

def parameter(pdm: ParamMagnet): pdm.Out

Description

Extracts a query parameter value from the requestrequest and provides it to the inner route as a String.

In the Scala API, parameter is an alias for parameters and you can use both directives to extract any number of parameter values. For a detailed description about how to extract one or more parameters see parameters.

See When to use which parameter directive? to understand when to use which directive.

Example

Scala
val route =
  parameter('color) { color =>
    complete(s"The color is '$color'")
  }

// tests:
Get("/?color=blue") ~> route ~> check {
  responseAs[String] shouldEqual "The color is 'blue'"
}

Get("/") ~> Route.seal(route) ~> check {
  status shouldEqual StatusCodes.NotFound
  responseAs[String] shouldEqual "Request is missing required query parameter 'color'"
}
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.parameter;

final Route route = parameter("color", color ->
  complete("The color is '" + color + "'")
);

// tests:
testRoute(route).run(HttpRequest.GET("/?color=blue"))
  .assertEntity("The color is 'blue'");

testRoute(route).run(HttpRequest.GET("/"))
  .assertStatusCode(StatusCodes.NOT_FOUND)
  .assertEntity("Request is missing required query parameter 'color'");
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.