Skip to content

Commit 3c3e138

Browse files
authored
docs(routes): expand on request/response APIs (#323)
1 parent 1cfc303 commit 3c3e138

File tree

1 file changed

+164
-3
lines changed

1 file changed

+164
-3
lines changed

docs/docs/basics/routes.md

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ Response onRequest(RequestContext context) {
1818
}
1919
```
2020

21-
## Request Context 🔗
21+
## Requests 📥
22+
23+
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
2226

2327
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)).
2428

@@ -29,12 +33,147 @@ Response onRequest(RequestContext context) {
2933
// Access the incoming request.
3034
final request = context.request;
3135
36+
// Do stuff with the incoming request...
37+
3238
// Return a response.
3339
return Response(body: 'Hello World');
3440
}
3541
```
3642

37-
## Custom Status Code 🆗
43+
### HTTP Method
44+
45+
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`.
138+
139+
```dart
140+
import 'package:dart_frog/dart_frog.dart';
141+
142+
Future<Response> onRequest(RequestContext context) async {
143+
// Access the incoming request.
144+
final request = context.request;
145+
146+
// Access the request body as a `String`.
147+
final body = await request.body();
148+
149+
return Response(body: 'The body is "$body".');
150+
}
151+
```
152+
153+
:::caution
154+
The request body can only be read once.
155+
:::
156+
157+
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
38177

39178
We can customize the status code of the response via the `statusCode` parameter on the `Response` object:
40179

@@ -46,7 +185,29 @@ Response onRequest(RequestContext context) {
46185
}
47186
```
48187

49-
## Returning JSON `{}`
188+
### Headers
189+
190+
We can customize the headers of the response via the `headers` parameter on the `Response` object:
191+
192+
```dart
193+
import 'package:dart_frog/dart_frog.dart';
194+
195+
Response onRequest(RequestContext context) {
196+
return Response(headers: {'hello': 'world'});
197+
}
198+
```
199+
200+
### Body
201+
202+
We've seen examples of returning a custom body via the default `Response` constructor:
203+
204+
```dart
205+
import 'package:dart_frog/dart_frog.dart';
206+
207+
Response onRequest(RequestContext context) {
208+
return Response(body: 'Hello World');
209+
}
210+
```
50211

51212
In addition, we can return JSON via the `Response.json` constructor:
52213

0 commit comments

Comments
 (0)