Skip to main content
Version: 2.x

RoutePattern

RoutePattern defines a pattern for matching routes by examining both the HTTP method and the path. In addition to specifying a method, patterns contain segment patterns, which can consist of literals, integers, longs, and other segment types.

A RoutePattern is composed of a Method and a PathCodec:

case class RoutePattern[A](method: Method, pathCodec: PathCodec[A])

To create a Route we need to create a RoutePattern and then bind it to a handler using the -> operator:

import zio.http._

Routes(
Method.GET / "health-check" -> Handler.ok,
)

In the above example, Method.Get / "health-check" represents a RoutePattern used to match incoming requests with the appropriate path and method.

Building RoutePatterns

Typically, the entry point for creating a route pattern is Method:

// GET /users
val pattern: RoutePattern[Unit] =
Method.GET / "users"

To match a path segment, various methods like string, int, long, and uuid are available.

For example, let's enhance the previous example to match a user id of type Int:

// GET /users/:user-id
val pattern2: RoutePattern[Int] =
Method.GET / "users" / int("user-id")

The type of the RoutePattern becomes Int because it matches a path segment of type Int.

Multiple path segments can be matched by combining multiple PathCodec values. Let's extend the example to match a post id of type String:

// GET /users/:user-id/posts/:post-id
val pattern2: RoutePattern[(Int, String)] =
Method.GET / "users" / int("user-id") / "posts" / string("post-id")

With more path segments, the type of the RoutePattern becomes a tuple of the types of the path segments, in this case, (Int, String).

Matching Methods

The Method data type represent an HTTP method, and it offers the following predefined HTTP methods: GET, POST, PUT, DELETE, PATCH, DELETE, TRACE, OPTIONS, HEAD, TRACE and CONNECT.

The METHOD.ANY is a wildcard method that matches any method.