Response class

The response returned by a Handler.

A Response encapsulates the HTTP status code, headers, and body content that will be sent back to the client.

Success Responses

// 200 OK - Standard success
Response.ok(body: Body.fromString('Success!'))

// 204 No Content - Success without body
Response.noContent()

HEAD Requests

Answer a HEAD request with the response GET would get. The adapter sends the body's length and type, and cancels the body's stream without reading it. Cancelling a stream listens to it first, so a stream from File.openRead opens and closes its file. A Body built with open makes its stream only when it is read, which a HEAD response never does:

Response.ok(
  body: Body(
    open: () => file.openRead().cast(),
    contentLength: size,
    bodyType: BodyType(mimeType: MimeType.pdf),
  ),
)

A plain Body.empty() sends Content-Length: 0.

A router answers HEAD by itself: a HEAD request to a path without a HEAD route runs its GET route.

Redirect Responses

// 301 Moved Permanently
Response.movedPermanently(Uri.parse('/new-location'))

// 302 Found - Temporary redirect
Response.found(Uri.parse('/temporary'))

// 303 See Other - Redirect after POST
Response.seeOther(Uri.parse('/success'))

Client Error Responses

// 400 Bad Request
Response.badRequest(body: Body.fromString('Invalid input'))

// 401 Unauthorized
Response.unauthorized(body: Body.fromString('Please log in'))

// 403 Forbidden
Response.forbidden(body: Body.fromString('Access denied'))

// 404 Not Found
Response.notFound(body: Body.fromString('Page not found'))

Server Error Responses

// 500 Internal Server Error
Response.internalServerError(body: Body.fromString('Server error'))

// 501 Not Implemented
Response.notImplemented(body: Body.fromString('Coming soon'))

JSON Response

final data = {'name': 'Alice', 'age': 30};
Response.ok(
  body: Body.fromString(
    jsonEncode(data),
    mimeType: MimeType.json,
  ),
)

HTML Response

Response.ok(
  body: Body.fromString(
    '<html><body><h1>Hello!</h1></body></html>',
    mimeType: MimeType.html,
  ),
)
Inheritance
Implemented types

Constructors

Response(int statusCode, {Body? body, Headers? headers})
Constructs an HTTP response with the given statusCode.
Response.badRequest({Body? body, Headers? headers})
Constructs a 400 Bad Request response.
Response.contentTooLarge({Body? body, Headers? headers})
Constructs a 413 Content Too Large response.
Response.forbidden({Body? body, Headers? headers})
Constructs a 403 Forbidden response.
Response.found(Uri location, {Body? body, Headers? headers})
Constructs a 302 Found response.
Response.internalServerError({Body? body, Headers? headers})
Constructs a 500 Internal Server Error response.
Response.movedPermanently(Uri location, {Body? body, Headers? headers})
Constructs a 301 Moved Permanently response.
Response.noContent({Headers? headers})
Constructs a 204 No Content response.
Response.notFound({Body? body, Headers? headers})
Constructs a 404 Not Found response.
Response.notImplemented({Body? body, Headers? headers})
Constructs a 501 Not Implemented response.
Response.notModified({Headers? headers})
Constructs a 304 Not Modified response.
Response.ok({Body? body, Headers? headers})
Constructs a 200 OK response.
Response.seeOther(Uri location, {Body? body, Headers? headers})
Constructs a 303 See Other response.
Response.unauthorized({Body? body, Headers? headers})
Constructs a 401 Unauthorized response.

Properties

body ↔ Body
The streaming body of the message.
getter/setter pairinherited
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
mimeType → MimeType?
Returns the MIME type from the Body-Type (Content-Type header), if available.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
statusCode → int
The HTTP status code of the response.
final

Methods

copyWith({Headers? headers, Body? body}) → Response
Creates a new Response by copying existing values and applying specified changes.
override
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

Operators

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