Request class

An HTTP request to be processed by a Relic Server application.

The Request object provides access to all information about an incoming HTTP request, including the method, URL, headers, query parameters, and body.

Usage Examples

// Basic request handling
router.get('/users/:id', (req) {
  // Access path parameters
  final id = req.pathParameters[#id];

  // Access HTTP method
  print(req.method); // Method.get

  // Access query parameters
  final sort = req.url.queryParameters['sort'];
  final filter = req.url.queryParameters['filter'];

  // Multiple values for same parameter
  // URL: /tags?tag=dart&tag=server
  final tags = req.url.queryParametersAll['tag'];
  // tags = ['dart', 'server']

  // Access headers
  final userAgent = req.headers.userAgent;

  return Response.ok(
    body: Body.fromString('User request'),
  );
});

// Reading request body
router.post('/api/data', (req) async {
  // Check if body exists
  if (req.isEmpty) {
    return Response.badRequest();
  }

  // Read as string
  final bodyText = await req.readAsString();

  // Parse JSON
  final data = jsonDecode(bodyText);
  return Response.ok();
});
Inheritance
Available extensions

Properties

body ↔ Body
The streaming body of the message.
getter/setter pairinherited
cancelled → Future<void>
Completes when the peer went away before the response finished.
no setter
connectionInfo → ConnectionInfo
Information about the IP connection carrying the request.
no setter
encoding → Encoding?
Returns the encoding specified in the Body-Type (Content-Type header), or null if not specified.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
headers → Headers
The HTTP headers associated with this message.
finalinherited
isEmpty → bool
Determines if the body is empty by checking the content length.
no setterinherited
matchedPath → NormalizedPath

Available on Request, provided by the RoutingRequestEx extension

The portion of the request path that was matched by the route.
no setter
method → Method
The HTTP request method, such as "GET" or "POST".
final
mimeType → MimeType?
Returns the MIME type from the Body-Type (Content-Type header), if available.
no setterinherited
pathParameters → PathParameters

Available on Request, provided by the PathParametersRequestEx extension

Typed path parameters extracted from the matched route.
no setter
protocol → HttpProtocol
The HTTP version the request arrived with.
final
queryParameters → QueryParameters

Available on Request, provided by the QueryParametersRequestEx extension

Typed query parameters extracted from the request URL.
no setter
rawPathParameters → Map<Symbol, String>

Available on Request, provided by the RoutingRequestEx extension

Raw path parameters extracted from the matched route.
no setter
remainingPath → NormalizedPath

Available on Request, provided by the RoutingRequestEx extension

The portion of the request path that was not consumed by the matched route.
no setter
router → RelicRouter?

Available on Request, provided by the RoutingRequestEx extension

The RelicRouter that routed this request, if available.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
target → RequestTarget
The path and query as received.
no setter
url → Uri
The original Uri for the request.
no setter

Methods

copyWith({Uri? url, Headers? headers, Body? body}) → Request
Creates a new Request by copying existing values and applying specified changes.
override
formData({FormLimits limits = FormLimits.defaults, UploadStorage? uploadStorage}) → Future<FormData>

Available on Request, provided by the FormRequestExtension extension

Parses a request body as form data based on its Content-Type.
forwardTo(Request request) → FutureOr<Result>

Available on Request, provided by the ForwardingRequestEx extension

Forwards request through the RelicRouter that routed this request.
multipart({FormLimits limits = FormLimits.defaults}) → Stream<MultipartPart>

Available on Request, provided by the FormRequestExtension extension

Streams a multipart/form-data request body as individual parts.
multipartForm({FormLimits limits = FormLimits.defaults, UploadStorage? uploadStorage}) → Future<MultipartFormData>

Available on Request, provided by the FormRequestExtension extension

Parses a multipart/form-data request body into aggregate form data.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read({int? maxLength}) → Stream<Uint8List>
Reads the body as a stream of bytes. Can only be called once.
inherited
readAsString({Encoding? encoding, int? maxLength}) → Future<String>
Reads the body as a string, decoding it using the specified or detected encoding. Defaults to utf8 if no encoding is provided or detected.
inherited
toString() → String
A string representation of this object.
inherited
urlEncodedForm({FormLimits limits = FormLimits.defaults}) → Future<UrlEncodedFormData>

Available on Request, provided by the FormRequestExtension extension

Parses an application/x-www-form-urlencoded request body.

Operators

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