ServerHttpResponse class abstract interface

The HTTP response being written for a request: status code, headers, cookies, then the body.

Status and headers can change until the response is committed, that is until the first body bytes, a flush or close; from then on they are on their way to the client (isCommitted). The body can be sent in parts, which is how server-sent events and large downloads stream.

Most controllers never touch it: they return a value or a ResponseEntity and the web starter writes the response. Declare a controller parameter of this type to write the answer yourself; a WebFilter or a HandlerMapping always works with it.

// A controller method that streams a CSV export line by line.
@GetMapping('/export.csv')
Future<void> export(ServerHttpResponse response) async {
  response
    ..statusCode = 200
    ..headers.contentType = 'text/csv; charset=utf-8';
  await for (final book in books.all()) {
    response.write('${book.isbn},${book.title}\n');
  }
  await response.close();
}

Spring (Java): ServerHttpResponse (Spring WebFlux), or HttpServletResponse on the servlet stack.

Implementers
Available extensions

Properties

done → Future<void>
Completes when the response is over: closed, or the client went away. A long-running stream (server-sent events) listens to it to stop producing.
no setter
hashCode → int
The hash code for this object.
no setterinherited
headers → HttpHeaders
The headers to send. Changes made after the response is committed (isCommitted) no longer reach the client.
no setter
isCommitted → bool
Whether the status and headers are already sent, which happens at the first body bytes, a flush or close. A filter that wants to change headers checks this first.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
statusCode ↔ int
The status code to send; 200 until set. Changing it after the response is committed has no effect.
getter/setter pair

Methods

add(List<int> bytes) → void
Appends bytes to the body; commits the response first if needed.
addCookie(ResponseCookie cookie) → void
Sends cookie to the client in a Set-Cookie header. Call it before the response is committed.
addStream(Stream<List<int>> stream) → Future<void>
Appends every chunk of stream to the body; completes when the stream is done. Use it to send a file or a proxied body without holding it in memory.
close() → Future<void>
Ends the response: commits it if nothing was written, then finishes the body. Every response must be closed once.
flush() → Future<void>
Sends what was written so far to the client now instead of when the buffer fills; used after each server-sent event so that it arrives at once.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(int status, String text, {String contentType = 'text/plain; charset=utf-8'}) → Future<void>

Available on ServerHttpResponse, provided by the ServerHttpResponseWriting extension

Sends status with text as the body (UTF-8, Content-Length set) and closes the response. contentType defaults to text/plain; charset=utf-8; pass another for JSON or HTML text.
toString() → String
A string representation of this object.
inherited
write(String text) → void
Appends text to the body, encoded in UTF-8 whatever the Content-Type says.

Operators

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