0
# HTTP Methods and Routing
1
2
Core HTTP method constants and route pattern matching functionality for defining web service routes using Scala's pattern matching syntax.
3
4
## Capabilities
5
6
### HTTP Method Constants
7
8
Standard HTTP method constants for route pattern matching.
9
10
```scala { .api }
11
/**
12
* HTTP GET method constant
13
*/
14
val GET: Method.GET.type
15
16
/**
17
* HTTP POST method constant
18
*/
19
val POST: Method.POST.type
20
21
/**
22
* HTTP PUT method constant
23
*/
24
val PUT: Method.PUT.type
25
26
/**
27
* HTTP DELETE method constant
28
*/
29
val DELETE: Method.DELETE.type
30
31
/**
32
* HTTP HEAD method constant
33
*/
34
val HEAD: Method.HEAD.type
35
36
/**
37
* HTTP OPTIONS method constant
38
*/
39
val OPTIONS: Method.OPTIONS.type
40
41
/**
42
* HTTP PATCH method constant
43
*/
44
val PATCH: Method.PATCH.type
45
46
/**
47
* HTTP CONNECT method constant
48
*/
49
val CONNECT: Method.CONNECT.type
50
51
/**
52
* HTTP TRACE method constant
53
*/
54
val TRACE: Method.TRACE.type
55
```
56
57
### Route Matching Operators
58
59
#### Method and Path Matcher (->)
60
61
Extracts HTTP method and path from a request for pattern matching.
62
63
```scala { .api }
64
/**
65
* HttpMethod extractor for pattern matching
66
* @param req HTTP request
67
* @returns Some tuple of (Method, Path)
68
*/
69
object -> {
70
def unapply[F[_]](req: Request[F]): Some[(Method, Path)]
71
}
72
73
/**
74
* Unicode alias for -> operator
75
* Note: Due to infix operation precedence, → has lower priority than /
76
* Use parentheses in pattern matching: case Method.GET → (Root / "path") => ...
77
*/
78
val → : impl.->.type
79
```
80
81
**Usage Examples:**
82
83
```scala
84
import org.http4s.dsl.io._
85
86
val routes = HttpRoutes.of[IO] {
87
case GET -> Root / "hello" =>
88
Ok("Hello!")
89
90
case POST -> Root / "users" / name =>
91
Created(s"User $name created")
92
93
// Using unicode operator (requires parentheses)
94
case req @ (GET → (Root / "info")) =>
95
Ok("Information")
96
}
97
```
98
99
#### Method Enumeration Matcher (->>)
100
101
Provides method enumeration with automatic error responses for unsupported methods.
102
103
```scala { .api }
104
/**
105
* Method enumeration extractor with automatic error handling
106
* Returns MethodNotAllowed (405) for defined but unmatched methods
107
* Returns NotImplemented (501) for undefined methods
108
*/
109
object ->> {
110
def unapply[F[_]: Applicative](req: Request[F]):
111
Some[(PartialFunction[Method, F[Response[F]]] => F[Response[F]], Path)]
112
}
113
```
114
115
**Usage Examples:**
116
117
```scala
118
val routes = HttpRoutes.of[IO] {
119
case withMethod ->> Root / "resource" => withMethod {
120
case GET => Ok("Getting resource")
121
case POST => Created("Resource created")
122
case PUT => Ok("Resource updated")
123
// Automatically returns 405 Method Not Allowed for DELETE, PATCH, etc.
124
// Returns 501 Not Implemented for non-standard methods
125
}
126
}
127
```
128
129
### Method Combination
130
131
#### Method Concatenation
132
133
Combine multiple HTTP methods for matching against any of them.
134
135
```scala { .api }
136
/**
137
* Method combination for matching multiple methods
138
*/
139
final class MethodConcat(val methods: Set[Method]) {
140
def unapply(method: Method): Option[Method]
141
}
142
143
/**
144
* Method syntax for creating combinations
145
*/
146
final class MethodOps(val method: Method) extends AnyVal {
147
def |(another: Method): MethodConcat
148
}
149
150
final class MethodConcatOps(val methods: MethodConcat) extends AnyVal {
151
def |(another: Method): MethodConcat
152
}
153
```
154
155
**Usage Examples:**
156
157
```scala
158
val routes = HttpRoutes.of[IO] {
159
// Match GET or HEAD requests
160
case (GET | HEAD) -> Root / "resource" =>
161
Ok("Resource content")
162
163
// Match multiple methods
164
case (POST | PUT | PATCH) -> Root / "resource" =>
165
Accepted("Resource modified")
166
}
167
```
168
169
### Method Integration
170
171
The HTTP methods integrate seamlessly with the request DSL:
172
173
```scala
174
// Import provides implicit conversions
175
import org.http4s.dsl.io._
176
177
// Methods are automatically available for pattern matching
178
val routes = HttpRoutes.of[IO] {
179
case GET -> Root / "status" => Ok("OK")
180
case POST -> Root / "data" :? QueryParam(param) =>
181
Ok(s"Received param: $param")
182
}
183
```