Request
The request gives you the input, files, headers and cookies that the client sent.
Typed input
Ask for a DTO in your controller. Dox reads the body (or the query for GET, HEAD and DELETE), checks it, and gives you a typed object:
Route.put('/users/{id}', (UserController c) => c.update);
@Inject()
class UserController {
Future<User?> update(int id, UpdateUser input) async {
User? user = await User().find(id);
user?.name = input.name;
await user?.save();
return user;
}
}
Bad input gets 422 before your method runs. See Validation.
Checking in the controller
To check the input at a later point, call validate with the DTO type:
For input you know only at runtime, use a fluent schema. See Fluent schemas:
Map<String, dynamic> data = await req.validateSchema(v.object(
<String, Schema<Object?>>{'email': v.string().email()}));
Raw input
Add a DoxRequest parameter to read the raw values. Dox does not check these values.
Retrieving an input value
Read one value from the query string or the body. The body wins when both have the key:
String? name = req.input('name');
String name = req.input('name', 'guest'); // default for a missing key
Use a dot key to read inside a map or a list:
A key that is in the input as it is, like 'a.b', wins over a dot key.
Retrieving all input
Get all values of the query string and the body:
Get only the body or only the query string:
Retrieving a part of the input
Get some keys, or all keys but some:
Checking if a value exists
filled gives true when the key is there and not empty. Only spaces, an empty list and an empty map count as empty:
exists gives true for a key with any value, also an empty string or null. missing is the opposite:
has gives true when the key is there and not empty. It does not read dot keys.
Reading booleans and numbers
boolean gives true for true, 1, on and yes, so it works for checkboxes:
integer gives the default when the key is missing or not a number:
Adding input
Add one value, or a map of values, for example in a middleware:
Lists and maps in forms
Form and query keys with brackets give lists and maps:
| Input | req.input(...) gives |
|---|---|
tags[]=a&tags[]=b |
req.input('tags') is ['a', 'b'] |
address[city]=Yangon |
req.input('address') is {'city': 'Yangon'} |
items[0][name]=x |
req.input('items') is [{'name': 'x'}] |
name=a&name=b (no brackets) |
req.input('name') is 'b', the last value |
Route params
Ask for a path param as a typed parameter, like int id above. See Controller: parameters.
The URL-decoded text values are also in req.param:
A value from route model binding comes from bound:
Form data (File)
An uploaded file is a RequestFile. The request must be multipart/form-data. Put the file in a DTO, so Dox checks its type and size:
@Dto()
class UploadImage {
@Image()
@MaxSize('5MB')
final RequestFile image;
const UploadImage({required this.image});
static const DtoSchema<UploadImage> schema = $UploadImageDto;
}
Then store it. store comes from the dox_storage package:
Future<Map<String, String>> uploadImage(UploadImage input) async {
String path = await input.image.store('images');
return <String, String>{'filename': path};
}
Save the file before you return
Dox deletes the temp file of a big upload when the response is sent.
Call store, moveTo or stream before your controller returns.
Files without a DTO
Get a file, or null when the field is not a file. Dox does not check it:
hasFile gives true when the field holds a file or a list of files. For a field with many files, use req.input('images'):
Reading and saving a file
A RequestFile tells you about the file:
file.filename; // name from the client
file.filetype; // content type, like video/mp4
file.extension; // mp4
file.length; // size in bytes
await file.size; // size in KB
file.head; // the first bytes, to find the real file type
Save it to storage, or move it:
await file.store('videos'); // gives the path
await file.moveTo('/data/videos/1.mp4'); // gives the new File
Read it in chunks with stream. bytes loads the full file in memory, so do not use it for big files:
file.file is the temp file on disk, or null when the upload is in memory. An upload up to 1 MB stays in memory.
Size limits
A body that is too big gets 413. Change the limits in AppConfig:
AppConfig(
maxBodySize: 10 * 1024 * 1024, // JSON and urlencoded, default 10 MB
maxMultipartSize: 100 * 1024 * 1024, // uploads, default 100 MB
);
Set the same limit in your proxy, like client_max_body_size in nginx.
Headers
Read one header, or all of them. A repeated header is joined with ', ':
There are short methods for common headers:
req.userAgent(); // User-Agent header
req.host(); // Host header
req.origin(); // Origin header, or null
req.referer(); // Referer header, or null
req.locale; // first language of Accept-Language, like 'th', or null
req.bearerToken(); // the token of 'Authorization: Bearer <token>', or null
Body type
Check the body type, or if the client wants a JSON answer:
req.isJson(); // true for a JSON body
req.isFormData(); // true for multipart/form-data
req.expectsJson(); // true for a JSON Accept header, JSON body or XHR
Method, path and URL
Read the method and the URL of the request:
req.method; // 'GET'
req.path(); // '/admin/users/5'
req.fullUrl(); // 'https://example.com/admin/users/5?tab=info'
req.uri; // the Uri
req.protocolVersion; // '1.1', or '2.0' for HTTP/2
Check where the request goes. * matches any text, and a route with no name never matches routeIs:
Cookies
Read a cookie. Dox encrypts cookies by default, so you get the decrypted value:
String? token = req.cookie('token');
String? raw = req.cookie('theme', decrypt: false); // value as sent
See Response to set a cookie.
Client IP and scheme
Behind a proxy, set trustedProxies in AppConfig, or req.ip() gives the proxy. See Deployment.
Other request data
These come from middleware:
req.requestId; // from RequestId, see Middleware
req.session; // from SessionMiddleware, see Session
req.hasSession; // true when the route has a session
req.auth; // the logged in user, see Authentication
See Middleware, Session and Authentication.
Send the response yourself
Write the response on req.httpRequest.response. After takeOverResponse, Dox sends nothing more for the request:
Future<void> handle(DoxRequest req) async {
req.takeOverResponse();
HttpResponse res = req.httpRequest.response;
res.write('done');
await res.close();
}
Your code must close the response. req.responseTakenOver gives true after the call.
Next steps
- DTO: typed and checked input.
- Validation: the rules for input.
- Response: send an answer.