Requests

Together, a route’s attribute and function signature specify what must be true about a request in order for the route’s handler to be called. You’ve already seen an example of this in action:

  1. #[get("/world")]
  2. fn handler() { .. }

This route indicates that it only matches against GET requests to the /world route. Rocket ensures that this is the case before handler is called. Of course, you can do much more than specify the method and path of a request. Among other things, you can ask Rocket to automatically validate:

  • The type of a dynamic path segment.
  • The type of many dynamic path segments.
  • The type of incoming data.
  • The types of query strings, forms, and form values.
  • The expected incoming or outgoing format of a request.
  • Any arbitrary, user-defined security or validation policies.

The route attribute and function signature work in tandem to describe these validations. Rocket’s code generation takes care of actually validating the properties. This section describes how to ask Rocket to validate against all of these properties and more.