You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All route handlers have access to information regarding the inbound request. In this section, we'll take a look at various ways in which we can interact with the inbound request.
24
+
25
+
### Request Context
22
26
23
27
All route handlers have access to a `RequestContext` which can be used to access the incoming request as well as dependencies provided to the request context ([see middleware](/docs/basics/middleware)).
A single route handler is responsible for handling inbound requests with any HTTP method. The HTTP method of the inbound request can be accessed via `context.request.method`.
46
+
47
+
```dart
48
+
import 'package:dart_frog/dart_frog.dart';
49
+
50
+
Response onRequest(RequestContext context) {
51
+
// Access the incoming request.
52
+
final request = context.request;
53
+
54
+
// Access the HTTP method.
55
+
final method = request.method.value;
56
+
57
+
return Response(body: 'This is a $method request.');
58
+
}
59
+
```
60
+
61
+
We can make a `GET` request to the above handler and we should see:
62
+
63
+
```
64
+
curl --request GET --url http://localhost:8080
65
+
66
+
This is a GET request.
67
+
```
68
+
69
+
We can make a `POST` request to the above handler and we should see:
70
+
71
+
```
72
+
curl --request POST --url http://localhost:8080
73
+
74
+
This is a POST request.
75
+
```
76
+
77
+
### Headers
78
+
79
+
We can access request headers via `context.request.headers`.
80
+
81
+
```dart
82
+
import 'package:dart_frog/dart_frog.dart';
83
+
84
+
Response onRequest(RequestContext context) {
85
+
// Access the incoming request.
86
+
final request = context.request;
87
+
88
+
// Access the headers as a `Map<String, String`.
89
+
final headers = request.headers;
90
+
91
+
// Do something with the headers...
92
+
93
+
return Response(body: 'Hello World');
94
+
}
95
+
```
96
+
97
+
### Query Parameters
98
+
99
+
We can access query parameters via `context.request.uri.queryParameters`.
100
+
101
+
```dart
102
+
import 'package:dart_frog/dart_frog.dart';
103
+
104
+
Response onRequest(RequestContext context) {
105
+
// Access the incoming request.
106
+
final request = context.request;
107
+
108
+
// Access the query parameters as a `Map<String, String`.
109
+
final params = request.uri.queryParameters;
110
+
111
+
// Get the value for the key `name`.
112
+
// Default to `there` if there is no query parameter.
113
+
final name = params['name'] ?? 'there';
114
+
115
+
return Response(body: 'Hi $name');
116
+
}
117
+
```
118
+
119
+
We can make a request to the above handler with no query parameters and we should see:
120
+
121
+
```
122
+
curl --request GET --url http://localhost:8080
123
+
124
+
Hi there
125
+
```
126
+
127
+
We can make a another request to the above handler with `?name=Dash` and we should see:
128
+
129
+
```
130
+
curl --request GET --url http://localhost:8080?name=Dash
131
+
132
+
Hi Dash
133
+
```
134
+
135
+
### Body
136
+
137
+
We can access the body of the incoming request via `context.request.body`.
We can make a request to the above handler with some data and we should see:
158
+
159
+
```
160
+
curl --request POST \
161
+
--url http://localhost:8080 \
162
+
--header 'Content-Type: text/plain' \
163
+
--data 'Hello!'
164
+
165
+
The body is "Hello!".
166
+
```
167
+
168
+
:::tip
169
+
When the `Content-Type` is `application/json`, you can use `context.request.json()` to read the contents of the request body as a `Map<String, dynamic>`.
170
+
:::
171
+
172
+
## Responses 📤
173
+
174
+
All route handlers must return an outbound response. In this section, we'll take a look at various ways in which we can create a custom response.
175
+
176
+
### Status Code
38
177
39
178
We can customize the status code of the response via the `statusCode` parameter on the `Response` object:
0 commit comments