Core HTTP method constants and route pattern matching functionality for defining web service routes using Scala's pattern matching syntax.
Standard HTTP method constants for route pattern matching.
/**
* HTTP GET method constant
*/
val GET: Method.GET.type
/**
* HTTP POST method constant
*/
val POST: Method.POST.type
/**
* HTTP PUT method constant
*/
val PUT: Method.PUT.type
/**
* HTTP DELETE method constant
*/
val DELETE: Method.DELETE.type
/**
* HTTP HEAD method constant
*/
val HEAD: Method.HEAD.type
/**
* HTTP OPTIONS method constant
*/
val OPTIONS: Method.OPTIONS.type
/**
* HTTP PATCH method constant
*/
val PATCH: Method.PATCH.type
/**
* HTTP CONNECT method constant
*/
val CONNECT: Method.CONNECT.type
/**
* HTTP TRACE method constant
*/
val TRACE: Method.TRACE.typeExtracts HTTP method and path from a request for pattern matching.
/**
* HttpMethod extractor for pattern matching
* @param req HTTP request
* @returns Some tuple of (Method, Path)
*/
object -> {
def unapply[F[_]](req: Request[F]): Some[(Method, Path)]
}
/**
* Unicode alias for -> operator
* Note: Due to infix operation precedence, → has lower priority than /
* Use parentheses in pattern matching: case Method.GET → (Root / "path") => ...
*/
val → : impl.->.typeUsage Examples:
import org.http4s.dsl.io._
val routes = HttpRoutes.of[IO] {
case GET -> Root / "hello" =>
Ok("Hello!")
case POST -> Root / "users" / name =>
Created(s"User $name created")
// Using unicode operator (requires parentheses)
case req @ (GET → (Root / "info")) =>
Ok("Information")
}Provides method enumeration with automatic error responses for unsupported methods.
/**
* Method enumeration extractor with automatic error handling
* Returns MethodNotAllowed (405) for defined but unmatched methods
* Returns NotImplemented (501) for undefined methods
*/
object ->> {
def unapply[F[_]: Applicative](req: Request[F]):
Some[(PartialFunction[Method, F[Response[F]]] => F[Response[F]], Path)]
}Usage Examples:
val routes = HttpRoutes.of[IO] {
case withMethod ->> Root / "resource" => withMethod {
case GET => Ok("Getting resource")
case POST => Created("Resource created")
case PUT => Ok("Resource updated")
// Automatically returns 405 Method Not Allowed for DELETE, PATCH, etc.
// Returns 501 Not Implemented for non-standard methods
}
}Combine multiple HTTP methods for matching against any of them.
/**
* Method combination for matching multiple methods
*/
final class MethodConcat(val methods: Set[Method]) {
def unapply(method: Method): Option[Method]
}
/**
* Method syntax for creating combinations
*/
final class MethodOps(val method: Method) extends AnyVal {
def |(another: Method): MethodConcat
}
final class MethodConcatOps(val methods: MethodConcat) extends AnyVal {
def |(another: Method): MethodConcat
}Usage Examples:
val routes = HttpRoutes.of[IO] {
// Match GET or HEAD requests
case (GET | HEAD) -> Root / "resource" =>
Ok("Resource content")
// Match multiple methods
case (POST | PUT | PATCH) -> Root / "resource" =>
Accepted("Resource modified")
}The HTTP methods integrate seamlessly with the request DSL:
// Import provides implicit conversions
import org.http4s.dsl.io._
// Methods are automatically available for pattern matching
val routes = HttpRoutes.of[IO] {
case GET -> Root / "status" => Ok("OK")
case POST -> Root / "data" :? QueryParam(param) =>
Ok(s"Received param: $param")
}