Response class final
Immutable HTTP response (BLUEPRINT_V2 §7.2.3).
Constructors
- Response({int status = 200, Headers? headers, ResponseBody body = const EmptyBody(), int? contentLengthOverride, WebSocketHandler? webSocket, bool webSocketCompression = true, int webSocketMaxPayloadBytes = defaultWebSocketMaxPayloadBytes})
-
Response.bytes(List<
int> bytes, {required String contentType, int status = 200, Map<String, String> ? headers}) -
Raw bytes with an explicit content type.
factory
- Response.created(Object? data, {String? location})
-
201 CreatedJSON response with optionalLocationheader.factory -
Response.html(String html, {int status = 200, Map<
String, String> ? headers}) -
HTML response.
factory
-
Response.json(Object? data, {int status = 200, Map<
String, String> ? headers, bool phpCompatible = false}) -
JSON response (
application/json).datamay contain maps, lists, numbers, strings, booleans,null, DateTime (ISO-8601 UTC), enums and JsonSerializable objects.factory -
Response.noContent({Map<
String, String> ? headers}) -
204 No Content.factory - Response.redirect(String location, {int status = 302})
-
Redirect response.
factory
-
Response.sse(Stream<
Object?> events, {Object? endStreamWith = '</stream>', Duration? heartbeat = const Duration(seconds: 15), Map<String, String> ? headers}) -
Server-sent events (Laravel's
response()->eventStream()): each item ofevents(an SseEvent, or a value sent as anupdateevent) is written as it comes, a: keep-alivecomment everyheartbeatkeeps proxies from closing an idle stream, andendStreamWith(</stream>, as Laravel;nullfor none) closes it. The stream also ends when the server starts draining, soEventSourcereconnects elsewhere. A client that went away is noticed at the next event or heartbeat (TCP reports nothing on an idle connection), which then cancelsevents.factory -
Response.stream(Stream<
List< stream, {required String contentType, int status = 200, int? contentLength, Map<int> >String, String> ? headers}) -
Streamed body with an explicit content type.
factory
-
Response.text(String body, {int status = 200, Map<
String, String> ? headers}) -
Plain text response.
factory
- Response.webSocket(WebSocketHandler handler, {bool compress = true, int maxPayloadBytes = defaultWebSocketMaxPayloadBytes})
-
Switches the connection to a WebSocket (
router.wsreturns this after the route's middleware passed);handlergets the socket inside the request's scope.compressfalse turns permessage-deflate off: no compression state per connection, which matters with many of them.factory
Properties
- body → ResponseBody
-
final
- contentLengthOverride → int?
-
Content length to advertise when the body was stripped (HEAD requests).
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- headers → Headers
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- status → int
-
final
- webSocket → WebSocketHandler?
-
Set by Response.webSocket: the server upgrades the connection and
hands the socket to this handler.
final
- webSocketCompression → bool
-
Whether the WebSocket offers permessage-deflate to clients that ask.
final
- webSocketMaxPayloadBytes → int
-
The largest frame the WebSocket accepts (uncompressed bytes); a
larger one fails the connection (defaultWebSocketMaxPayloadBytes).
final
Methods
-
bodyBytes(
) → Future< List< int> > - Collects the body as bytes (consumes stream bodies).
-
bodyText(
) → Future< String> - Collects the body as a UTF-8 string.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
withCookie(
String name, String value, {Duration? maxAge, String path = '/', String? domain, bool secure = false, bool httpOnly = true, String? sameSite = 'lax'}) → Response -
Copy with a
Set-Cookieheader added (Laravel'swithCookie), in the format Laravel writes.maxAgenullmakes a session cookie; the defaults are Laravel's (path=/,httponly,samesite=lax). The value is sent as is: Laravel'sEncryptCookiesis not applied. -
withHeader(
String name, String value) → Response -
Copy with
nameset tovalue. -
withHeaderList(
String name, List< String> values) → Response -
Copy with every value of
namereplaced byvalues(a multi-valued header such asSet-Cookie); an empty list removes the header. -
withHeaders(
Map< String, String> values) → Response -
Copy with all
valuesset. -
withoutBody(
) → Response -
Copy without a body (used for
HEAD), keeping the advertised length. -
withoutCookie(
String name, {String path = '/', String? domain}) → Response -
Copy that tells the client to delete the cookie
name(Laravel'swithoutCookie/Cookie::forget);pathanddomainmust match the ones it was set with. -
withStatus(
int status) → Response -
Copy with a different
status. -
writeTo(
HttpResponse out) → Future< void> -
Writes this response to a
dart:ioHttpResponse and closes it.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
encodeJson(
Object? data) → String -
Encodes
datawith the framework's JSON conventions.
Constants
- defaultWebSocketMaxPayloadBytes → const int
-
The largest WebSocket frame a route accepts unless it says otherwise:
1 MiB.
dart:iosets no limit of its own (its default is the largest integer unless the build definesdart.io.default.ws.max.payload.length), so one client could make a worker buffer a frame of any size.