Context class

Per-request context object that encapsulates all the data corresponding to a HTTP request and provides a way to write response. It provides various convenience methods and getters to access HTTP request data:

  1. Request object (req)
  2. Response object (response)
  3. Path parameters (pathParams)
  4. Query parameters (query)
  5. Body parsers (body, bodyAsText, bodyAsStream, bodyAsJson, bodyAsFormData, bodyAsUrlEncodedForm)
  6. Route variables (getVariable)
  7. Interceptors (after, before)
  8. Session object (session)

Example showing how to access query parameters using Context object. int add(Context ctx) => ctx.query.getInt('a') + ctx.query.getInt('b');

Constructors

Context(Request req, {SessionManager? sessionManager, required Logger log, required Map<Type, UserFetcher<AuthorizationUser>> userFetchers, required List<RouteInterceptor> before, required List<RouteInterceptor> after, required List<ExceptionHandler> onException, DateTime? at})

Properties

accepts Map<String, MimeType>
Returns mime types of the response accepted by the client of the HTTP request.
no setter
acceptsHtml bool
Returns true if the client accepts the response body in HTML format.
no setter
acceptsJson bool
Returns true if the client accepts the response body in JSON format.
no setter
after List<RouteInterceptor>
Interceptors that shall be executed after route handler is executed.
final
at DateTime
When the request arrived
final
before List<RouteInterceptor>
Interceptors that shall be executed before route handler is executed.
final
body Future<List<int>>
Returns body of HTTP request as bytes.
no setter
bodyAsStream Future<Stream<List<int>>>
Returns the body of HTTP request as stream of bytes.
no setter
contentType ContentType?
no setter
cookies Map<String, Cookie>
Returns cookies set in HTTP request.
no setter
hashCode int
The hash code for this object.
no setterinherited
headers HttpHeaders
Headers in the HTTP request.
no setter
isFormData bool
Returns true if the mime type of HTTP request is form-data.
no setter
isJson bool
Returns true if the mime type of HTTP request is JSON.
no setter
isUrlEncodedForm bool
Returns true if the mime type of HTTP request is url-encoded-form.
no setter
log → Logger
Logger that can be used to log from middlerware and route handlers.
final
method String
Method of the HTTP request
no setter
mimeType MimeType
Returns mime type of the HTTP request
no setter
onException List<ExceptionHandler>
Exception handlers executed if there is an exception during the execution of the route.
final
parsedSession Session?
Parsed session. Returns null, if the session is not parsed yet.
no setter
path String
Path of the HTTP request
no setter
pathParams PathParams
Path parameters
final
pathSegments List<String>
Path segments of the HTTP request
no setter
query QueryParams
Returns query parameters of the request
no setter
req Request
Request object of the current HTTP request.
final
response Response
getter/setter pair
route Route?
The registered route that matched the HTTP request.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
session Future<Session?>
The session for the given request.
no setter
sessionManager SessionManager?
Session manager to parse and write session data.
getter/setter pair
sessionNeedsUpdate bool
Does the session need update?
no setter
uri Uri
Uri of the HTTP request
no setter
userFetchers Map<Type, UserFetcher<AuthorizationUser>>
User fetchers for authentication and authorization.
final

Methods

addVariable(dynamic value, {required String id}) → void
Adds a named variable to the context
addVariableByType(dynamic value) → void
Adds a named variable to the context
addVariables(Map<String, dynamic> variables) → void
Adds a named variables to the context
addVariablesByType(List values) → void
authHeader(String scheme) String?
Returns auth header for the requested scheme.
bodyAsFormData() Future<Map<String, FormField>>
Decodes multipart/form-data body
bodyAsJson<T, F>({Encoding encoding = conv.utf8, Converter<T, F>? convert, Type? type}) Future<T>
Decodes JSON body of the request
bodyAsJsonList<T, F>({Encoding encoding = conv.utf8, Converter<T, F>? convert, Type? type}) Future<List<T>?>
Decodes JSON body of the request as List
bodyAsJsonMap({Encoding encoding = conv.utf8}) Future<Map>
Decodes JSON body of the request as Map
bodyAsMap({Encoding encoding = conv.utf8}) Future<Map?>
bodyAsText([Encoding encoding = conv.utf8]) Future<String>
Returns body as text
bodyAsUrlEncodedForm({Encoding encoding = conv.utf8}) Future<Map<String, String>>
Decodes url-encoded form from the body and returns the form as Map<String, String>.
bodyTo<T>(Converter<T, dynamic> converter, {Encoding encoding = conv.utf8}) Future<T>
Converts the body to typ T.
execute() Future<void>
Executes the route with this Context.
getBinaryFile(String field) Future<BinaryFileFormField?>
Returns file for given field in form-data body. Returns null, if the field is not found, not a file field or body is not form-data.
getFile<T>(String field) Future<FileFormField<T>?>
Returns file for given field in form-data body. Returns null, if the field is not found, not a file field or body is not form-data.
getTextFile(String field) Future<TextFileFormField?>
Returns file for given field in form-data body. Returns null, if the field is not found, not a file field or body is not form-data.
getVariable<T>({required String id}) → T
Returns variable by id
getVariableByType<T>({T? orElse}) → T?
Returns variable by type
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited