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:
- Request object (req)
- Response object (response)
- Path parameters (pathParams)
- Query parameters (query)
- Body parsers (body, bodyAsText, bodyAsStream, bodyAsJson, bodyAsFormData, bodyAsUrlEncodedForm)
- Route variables (getVariable)
- Interceptors (after, before)
- 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< userFetchers, List<AuthorizationUser> >RouteInterceptor> before, List<RouteInterceptor> after, List<ExceptionHandler> onException, Map<String, CodecRepo> serializers, DateTime at})
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
- 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.
read-only
-
bodyAsStream
→ Future<
Stream< List< >int> > -
Returns the body of HTTP request as stream of bytes.
read-only
-
Returns cookies set in HTTP request.
read-only
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- 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
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
-
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
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
. IfmimeType
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. [...]
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
serializerFor<
T> (Type type, {String mimeType}) → Serializer< T> -
Returns serializer for given object
type
andmimeType
. [...] -
toString(
) → String -
A string representation of this object. [...]
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator. [...]
inherited