or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

http-methods.mdindex.mdpath-matching.mdresponse-generation.md

http-methods.mddocs/

0

# HTTP Methods

1

2

HTTP method constants and operations for pattern matching in http4s routes. These provide type-safe method matching and combination capabilities.

3

4

## Capabilities

5

6

### Standard HTTP Methods

7

8

Constants for all standard HTTP methods used in RESTful services.

9

10

```scala { .api }

11

/**

12

* Standard HTTP method constants for pattern matching

13

*/

14

val GET: Method.GET.type

15

val HEAD: Method.HEAD.type

16

val POST: Method.POST.type

17

val PUT: Method.PUT.type

18

val DELETE: Method.DELETE.type

19

val CONNECT: Method.CONNECT.type

20

val OPTIONS: Method.OPTIONS.type

21

val TRACE: Method.TRACE.type

22

val PATCH: Method.PATCH.type

23

```

24

25

**Usage Examples:**

26

27

```scala

28

import org.http4s.dsl.io._

29

import org.http4s.HttpRoutes

30

import cats.effect.IO

31

32

val routes = HttpRoutes.of[IO] {

33

case GET -> Root / "users" =>

34

Ok("List users")

35

36

case POST -> Root / "users" =>

37

Ok("Create user")

38

39

case PUT -> Root / "users" / IntVar(id) =>

40

Ok(s"Update user $id")

41

42

case DELETE -> Root / "users" / IntVar(id) =>

43

Ok(s"Delete user $id")

44

}

45

```

46

47

### Method Combination

48

49

Combine multiple HTTP methods for shared route handling using the `|` operator.

50

51

```scala { .api }

52

/**

53

* Method combination for OR matching multiple HTTP methods

54

*/

55

final class MethodConcat(val methods: Set[Method]) {

56

/** Check if the given method matches any in this combination */

57

def unapply(method: Method): Option[Method]

58

}

59

60

final class MethodOps(val method: Method) extends AnyVal {

61

/** Combine this method with another to create a MethodConcat */

62

def |(another: Method): MethodConcat

63

}

64

65

final class MethodConcatOps(val methods: MethodConcat) extends AnyVal {

66

/** Add another method to this combination */

67

def |(another: Method): MethodConcat

68

}

69

```

70

71

**Usage Examples:**

72

73

```scala

74

import org.http4s.dsl.io._

75

76

val routes = HttpRoutes.of[IO] {

77

// Handle GET or POST on the same endpoint

78

case req @ (GET | POST) -> Root / "api" / "data" =>

79

req.method match {

80

case GET => Ok("Getting data")

81

case POST => Ok("Creating data")

82

}

83

84

// Handle multiple methods with different logic

85

case req @ (PUT | PATCH) -> Root / "users" / IntVar(id) =>

86

req.method match {

87

case PUT => Ok(s"Replacing user $id")

88

case PATCH => Ok(s"Updating user $id")

89

}

90

91

// Complex method combinations

92

case req @ (GET | POST | PUT) -> Root / "resources" =>

93

req.method match {

94

case GET => Ok("List resources")

95

case POST => Created("Resource created")

96

case PUT => Ok("Resource updated")

97

}

98

}

99

```

100

101

### Method-Path Extraction

102

103

Extract both HTTP method and path information from requests using the `->` operator.

104

105

```scala { .api }

106

/**

107

* HTTP method and path extractor for pattern matching

108

*/

109

object -> {

110

/** Extract method and path from a request */

111

def unapply[F[_]](req: Request[F]): Some[(Method, Path)]

112

}

113

114

/** Unicode alias for -> operator */

115

val → : impl.->.type

116

```

117

118

**Usage Examples:**

119

120

```scala

121

import org.http4s.dsl.io._

122

123

val routes = HttpRoutes.of[IO] {

124

// Standard arrow operator

125

case GET -> Root / "hello" =>

126

Ok("Hello!")

127

128

// Unicode arrow (lower precedence - use parentheses)

129

case req @ (Method.GET → (Root / "test.json")) =>

130

Ok("Test JSON")

131

132

// Extract method and path separately

133

case req =>

134

req match {

135

case method -> path =>

136

Ok(s"Method: $method, Path: $path")

137

}

138

}

139

```

140

141

### Advanced Method Matching

142

143

The DSL also provides the `->>` operator for comprehensive method handling with automatic error responses.

144

145

```scala { .api }

146

/**

147

* Method enumeration extractor with automatic error responses

148

*/

149

object ->> {

150

def unapply[F[_]: Applicative](

151

req: Request[F]

152

): Some[(PartialFunction[Method, F[Response[F]]] => F[Response[F]], Path)]

153

}

154

```

155

156

**Usage Examples:**

157

158

```scala

159

import org.http4s.dsl.io._

160

import cats.effect.IO

161

162

val routes = HttpRoutes.of[IO] {

163

case withMethod ->> Root / "api" / "resource" => withMethod {

164

case GET => Ok("Get resource")

165

case POST => Created("Created resource")

166

case PUT => Ok("Updated resource")

167

// Automatically returns 405 Method Not Allowed for other methods

168

// with proper Allow header listing supported methods

169

}

170

}

171

```

172

173

## Error Handling

174

175

The DSL automatically handles method-related HTTP errors:

176

177

- **405 Method Not Allowed**: Returned by `->>` when method is not handled, includes `Allow` header with supported methods

178

- **501 Not Implemented**: Returned by `->>` for unrecognized HTTP methods

179

180

## Type Safety Notes

181

182

- All method constants are singleton types, ensuring compile-time safety

183

- Method combinations preserve type information for pattern matching

184

- The `->` extractor works with any effect type `F[_]`

185

- Method matching integrates seamlessly with path and query parameter extraction