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 Created JSON response with optional Location header.
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). data may 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 of events (an SseEvent, or a value sent as an update event) is written as it comes, a : keep-alive comment every heartbeat keeps proxies from closing an idle stream, and endStreamWith (</stream>, as Laravel; null for none) closes it. The stream also ends when the server starts draining, so EventSource reconnects elsewhere. A client that went away is noticed at the next event or heartbeat (TCP reports nothing on an idle connection), which then cancels events.
factory
Response.stream(Stream<List<int>> stream, {required String contentType, int status = 200, int? contentLength, Map<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.ws returns this after the route's middleware passed); handler gets the socket inside the request's scope. compress false 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-Cookie header added (Laravel's withCookie), in the format Laravel writes. maxAge null makes a session cookie; the defaults are Laravel's (path=/, httponly, samesite=lax). The value is sent as is: Laravel's EncryptCookies is not applied.
withHeader(String name, String value) → Response
Copy with name set to value.
withHeaderList(String name, List<String> values) → Response
Copy with every value of name replaced by values (a multi-valued header such as Set-Cookie); an empty list removes the header.
withHeaders(Map<String, String> values) → Response
Copy with all values set.
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's withoutCookie / Cookie::forget); path and domain must 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:io HttpResponse and closes it.

Operators

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

Static Methods

encodeJson(Object? data) → String
Encodes data with the framework's JSON conventions.

Constants

defaultWebSocketMaxPayloadBytes → const int
The largest WebSocket frame a route accepts unless it says otherwise: 1 MiB. dart:io sets no limit of its own (its default is the largest integer unless the build defines dart.io.default.ws.max.payload.length), so one client could make a worker buffer a frame of any size.