Request and response
This page lists the members of DoxRequest, DoxResponse and the response helpers.
DoxRequest
A controller and an IDoxNextMiddleware get a DoxRequest. An IDoxMiddleware gets an IDoxRequest, with fewer members. The last column shows its members.
| Member |
Returns |
What it gives |
On IDoxRequest |
validate<T>({Source? source}) |
Future<T> |
The request checked with the DTO T. Bad input gives 422. Throws StateError when T is not in AppConfig.dtos. |
no |
validateSchema<O>(Schema<O> schema) |
Future<O> |
Query and body checked with a fluent dox_validation schema. |
no |
all() |
Map<String, dynamic> |
Query and body together. Body wins on the same key. |
yes |
only(List<String> keys) |
Map<String, dynamic> |
Only these keys from all(). A missing key gives null. |
yes |
input(String key, [dynamic defaultValue]) |
dynamic |
One value from all(). A dot key like user.name or items.0.id reads inside maps and lists. You get the default when the key is not there or is null. IDoxRequest has no default value. |
yes |
except(List<String> keys) |
Map<String, dynamic> |
All values from all(), without these keys. |
no |
has(String key) |
bool |
true when the key is there and its text is not empty. |
yes |
exists(String key) |
bool |
true when the key is there, with any value (also '' or null). |
no |
missing(String key) |
bool |
true when the key is not there. |
no |
filled(String key) |
bool |
true when the key is there and not empty. Dot keys work. |
no |
boolean(String key, [bool defaultValue = false]) |
bool |
true for true, 1, on and yes. |
no |
integer(String key, [int defaultValue = 0]) |
int |
The value as an int. The default is used when the key is not there or is not a number. |
no |
file(String key) |
RequestFile? |
The uploaded file, or null when the field is not a file. |
no |
hasFile(String key) |
bool |
true when the field holds a file or a list of files. |
no |
add(String key, dynamic value) |
void |
Adds a value to all() and body. |
yes |
merge(Map<String, dynamic> values) |
void |
Adds many values to all() and body. |
yes |
body |
Map<String, dynamic> |
The parsed body (JSON or form fields). Files are RequestFile. |
no |
query |
Map<String, dynamic> |
The query string. |
no |
param |
Map<String, dynamic> |
The route params, like {'id': '5'}. |
no |
pathParams |
Map<String, String> |
The route params as text. null values are left out. |
no |
bound<T>(String param) |
T |
The value that Route.bind made for this route param. Throws StateError when there is none. |
no |
requestBody |
RequestBody |
The body as it was read, for DTOs. |
no |
isJson() |
bool |
The content type is JSON. |
yes |
isFormData() |
bool |
The content type is form data (multipart/form-data or urlencoded). |
yes |
toJson() |
Map<String, dynamic> |
all() with files replaced by their file name. Lets jsonEncode(req) work. |
yes |
Headers, cookies and client
| Member |
Returns |
What it gives |
On IDoxRequest |
header(String key) |
String? |
One header. null when it is not there. |
yes |
headers |
Map<String, dynamic> |
Every header. Many values of one name are joined. |
yes |
httpHeaders |
HttpHeaders |
The dart:io headers object. |
no |
contentType |
ContentType? |
The parsed Content-Type. |
yes |
bearerToken() |
String? |
The token of Authorization: Bearer <token>, or null. |
no |
cookie(String key, {bool decrypt = true}) |
String? |
One cookie. Decrypted with appKey unless decrypt: false. |
yes |
ip() |
String |
The client IP, or unknown. From X-Forwarded-For only behind a trusted proxy. |
yes |
scheme() |
String |
http or https. It is https when AppConfig.tls is set. Behind a trusted proxy, it comes from X-Forwarded-Proto. |
no |
isSecure() |
bool |
scheme() is https. |
no |
clientIp |
String? |
The client IP, or null. |
no |
userAgent() |
String |
The User-Agent header, or unknown. |
yes |
host() |
String |
The Host header, or unknown. |
yes |
origin() |
String? |
The Origin header. |
yes |
referer() |
String? |
The Referer header. |
yes |
locale |
String? |
The first language of Accept-Language, like th. null when there is none or it is *. |
no |
auth |
IAuth? |
The auth object set by AuthMiddleware. req.auth?.id() gives the user id. It is null for a guest. |
yes |
expectsJson() |
bool |
The client wants JSON: an Accept header with json, a JSON body, or X-Requested-With: XMLHttpRequest. An Inertia request is not. |
no |
DoxRequest.expectsJsonFor(HttpHeaders headers) |
bool |
The same check as expectsJson(), for raw headers. A static method. |
no |
requestId |
String? |
The id that the RequestId middleware set, or null. |
no |
protocolVersion |
String |
The HTTP version: 1.0, 1.1 or 2.0. |
no |
Session
| Member |
Returns |
What it gives |
On IDoxRequest |
session |
Session |
The session of the request. Needs SessionMiddleware. Throws StateError without it. |
no |
hasSession |
bool |
true when the request has a session. |
no |
Route and raw request
| Member |
Returns |
What it gives |
On IDoxRequest |
method |
String |
The HTTP method in upper case. |
yes |
uri |
Uri |
The request URI. |
yes |
route |
RouteData |
The matched route. |
no |
getRouteData() |
RouteData |
Same as route. |
yes |
path() |
String |
The request path, like /admin/users/5. |
no |
fullUrl() |
String |
The full URL with the query string. |
no |
pathIs(String pattern) |
bool |
true when the path matches. * matches any text. |
no |
routeIs(String pattern) |
bool |
true when the route name matches. It is false for a route with no name. |
no |
getRouteIdentifier() |
String |
A key for the route (base64 of <method>|<domain><path>). Useful for a cache or rate limit key. |
yes |
httpRequest |
HttpRequest |
The dart:io request. Use it for raw access. |
yes |
takeOverResponse() |
void |
Your handler sends the response itself on httpRequest.response, and Dox sends nothing more. Your code must close the response. |
no |
responseTakenOver |
bool |
True after takeOverResponse(). |
no |
DoxResponse
Each setter returns the same DoxResponse, so you can chain the calls.
| Member |
Returns |
What it does |
DoxResponse(dynamic content) |
DoxResponse |
The constructor. content can be a String, number, Map, List, a JSON serializable object, a Stream<List<int>>, a File, a StreamFile or a DownloadableFile. |
statusCode(int code) |
DoxResponse |
Sets the status. Default 200. |
status |
int? |
The status set with statusCode(), or null (which means 200). |
content(dynamic content) |
DoxResponse |
Replaces the content. |
getContent() |
dynamic |
The content as it was given. |
stream(Stream<List<int>> stream) |
DoxResponse |
Sets a byte stream as content. |
contentType(ContentType type) |
DoxResponse |
Sets Content-Type. A File without it gets the type from its extension. |
header(String key, dynamic value, {bool? replace}) |
DoxResponse |
Sets one header. A second call with the same key, in any case, replaces the value. A value that a middleware already put on the response stays, unless replace is true. When replace is null, CoreFlags.replaceHeaders decides. |
headers |
Map<String, dynamic> |
The headers set so far, by name. |
withHeaders(Map<String, dynamic> values) |
DoxResponse |
Adds these headers. A key replaces the header with the same name, in any case. Other headers stay. |
cookie(DoxCookie cookie, {bool setExpire = false}) |
DoxResponse |
Adds a Set-Cookie header. setExpire: true sends the cookie as expired, which removes it. |
cache(Duration duration) |
DoxResponse |
Sets Cache-Control: max-age=<seconds>. |
Response helpers
These top level functions give a DoxResponse.
| Function |
What it does |
response([dynamic content]) |
Same as DoxResponse(content). |
redirect(String url, {int status = 302}) |
A redirect to url. It sets the Location header. |
back(DoxRequest req, {String fallback = '/'}) |
A 302 redirect to the Referer header, or to fallback when there is none. |
How the content is sent
A controller can return a plain value or a DoxResponse:
| Content |
Sent as |
null |
an empty body |
String |
text, as it is |
Map, List, number, bool, object with toJson() |
JSON, Content-Type: application/json |
DateTime |
its ISO 8601 text |
File |
streamed, with Content-Length, Accept-Ranges and one Range supported |
StreamFile |
streamed with its content type. An HTML, XML or JavaScript type is sent as a download, in a sandbox. |
DownloadableFile |
streamed with Content-Disposition: attachment; filename="<name>" |
Stream<List<int>> |
streamed chunk by chunk |
IHttpException |
its status and toResponse(), see HTTP exceptions |
other Exception or Error |
500 with {"code": "server_error", "message": "Server Error"}. In development or local, the body is the error text. |
Next steps
- Request: read the input of a request, with examples.
- Response: send a response, with examples.
- HTTP exceptions: the error classes and their status codes.