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, Logger log, Map<Type, UserFetcher<AuthorizationUser>> userFetchers, List<RouteInterceptor> before, List<RouteInterceptor> after, List<ExceptionHandler> onException, Map<String, CodecRepo> serializers })

Properties

accepts → Map<String, MimeType>
Returns mime types of the response accepted by the client of the HTTP request.
read-only
acceptsHtml → bool
Returns true if the client accepts the response body in HTML format.
read-only
acceptsJson → bool
Returns true if the client accepts the response body in JSON format.
read-only
after → List<RouteInterceptor>
Interceptors that shall be executed after route handler is executed. [...]
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.
read-only
bodyAsStream → Future<Stream<List<int>>>
Returns the body of HTTP request as stream of bytes.
read-only
cookies → Map<String, Cookie>
Returns cookies set in HTTP request.
read-only
headers → HttpHeaders
Headers in the HTTP request.
read-only
isFormData → bool
Returns true if the mime type of HTTP request is form-data.
read-only
isJson → bool
Returns true if the mime type of HTTP request is JSON.
read-only
isUrlEncodedForm → bool
Returns true if the mime type of HTTP request is url-encoded-form.
read-only
log Logger
Logger that can be used to log from middlerware and route handlers.
final
method → String
Method of the HTTP request
read-only
mimeType MimeType
Returns mime type of the HTTP request
read-only
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.
read-only
path → String
Path of the HTTP request
read-only
pathParams PathParams
Path parameters [...]
final
pathSegments → List<String>
Path segments of the HTTP request
read-only
query QueryParams
Returns query parameters of the request [...]
read-only
req Request
Request object of the current HTTP request. [...]
final
response Response
read / write
route Route
The registered route that matched the HTTP request.
read / write
session → Future<Session>
The session for the given request. [...]
read-only
sessionManager SessionManager
Session manager to parse and write session data.
read / write
sessionNeedsUpdate → bool
Does the session need update?
read-only
uri → Uri
Uri of the HTTP request
read-only
userFetchers → Map<Type, UserFetcher<AuthorizationUser>>
User fetchers for authentication and authorization.
final
hashCode → int
The hash code for this object.
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

addVariable<T>(T value, { String id }) → void
Adds variable by type and id
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>. [...]
bodyDecode<T>() → Future<T>
Deserializes body by mimetype using _serializers
bodyTo<T>(Converter<T, dynamic> convert, { Encoding encoding: conv.utf8 }) → Future<T>
Converts the body to typ T.
codecFor({String mimeType }) CodecRepo
Returns CodecRepo for the requested mimeType. If mimeType is null, the mime type of request is used.
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>({String id, Type type }) → T
Gets variable by type and id. [...]
serializerFor<T>(Type type, { String mimeType }) Serializer<T>
Returns serializer for given object type and mimeType. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator.
inherited