description

# HttpResponse class abstract interface

An HTTP response, which returns the headers and data from the server to the client in response to an HTTP request.

Every HttpRequest object provides access to the associated HttpResponse object through the `response` property. The server sends its response to the client by writing to the HttpResponse object.

## Writing the response

This class implements IOSink. After the header has been set up, the methods from IOSink, such as `writeln()`, can be used to write the body of the HTTP response. Use the `close()` method to close the response and send it to the client.

    server.listen((HttpRequest request) {
      request.response.write('Hello, world!');
      request.response.close();
    });

When one of the IOSink methods is used for the first time, the request header is sent. Calling any methods that change the header after it is sent throws an exception.

If no "Content-Type" header is set then a default of "text/plain; charset=utf-8" is used and string data written to the IOSink will be encoded using UTF-8.

## Setting the headers

The HttpResponse object has a number of properties for setting up the HTTP headers of the response. When writing string data through the IOSink, the encoding used is determined from the "charset" parameter of the "Content-Type" header.

    HttpResponse response = ...
    response.headers.contentType
        = ContentType("application", "json", charset: "utf-8");
    response.write(...);  // Strings written will be UTF-8 encoded.

If no charset is provided the default of ISO-8859-1 (Latin 1) will be used.

    HttpResponse response = ...
    response.headers.add(HttpHeaders.contentTypeHeader, "text/plain");
    response.write(...);  // Strings written will be ISO-8859-1 encoded.

If a charset is provided but it is not recognized, then the "Content-Type" header will include that charset but string data will be encoded using ISO-8859-1 (Latin 1).

Implemented types
  - IOSink

## Properties

bufferOutput ↔ bool
  Gets or sets if the HttpResponse should buffer output.
  getter/setter pair
connectionInfo → HttpConnectionInfo?
  Gets information about the client connection. Returns `null` if the socket is not available.
  no setter
contentLength ↔ int
  Gets and sets the content length of the response. If the size of the response is not known in advance set the content length to -1, which is also the default if not set.
  getter/setter pair
cookies → List<Cookie>
  Cookies to set in the client (in the 'set-cookie' header).
  no setter
deadline ↔ Duration?
  Set and get the deadline for the response. The deadline is timed from the time it's set. Setting a new deadline will override any previous deadline. When a deadline is exceeded, the response will be closed and any further data ignored.
  getter/setter pair
done → Future
  A future that will complete when the consumer closes, or when an error occurs.
  no setter inherited
encoding ↔ Encoding
  The Encoding used when writing strings.
  getter/setter pair inherited
hashCode → int
  The hash code for this object.
  no setter inherited
headers → HttpHeaders
  Returns the response headers.
  no setter
persistentConnection ↔ bool
  Gets and sets the persistent connection state. The initial value of this property is the persistent connection state from the request.
  getter/setter pair
reasonPhrase ↔ String
  The reason phrase for the response.
  getter/setter pair
runtimeType → Type
  A representation of the runtime type of the object.
  no setter inherited
statusCode ↔ int
  The status code of the response.
  getter/setter pair

## Methods

add(List<int> data) → void
  Adds byte `data` to the target consumer, ignoring encoding.
  inherited
addError(Object error, [StackTrace? stackTrace]) → void
  Passes the error to the target consumer as an error event.
  inherited
addStream(Stream<List<int>> stream) → Future
  Adds all elements of the given `stream`.
  inherited
close() → Future
  Close the target consumer.
  inherited
detachSocket({bool writeHeaders = true}) → Future<Socket>
  Detaches the underlying socket from the HTTP server. When the socket is detached the HTTP server will no longer perform any operations on it.
flush() → Future
  Returns a Future that completes once all buffered data is accepted by the underlying StreamConsumer.
  inherited
noSuchMethod(Invocation invocation) → dynamic
  Invoked when a nonexistent method or property is accessed.
  inherited
redirect(Uri location, {int status = HttpStatus.movedTemporarily}) → Future
  Respond with a redirect to `location`.
toString() → String
  A string representation of this object.
  inherited
write(Object? object) → void
  Converts `object` to a String by invoking Object.toString and adds the encoding of the result to the target consumer.
  inherited
writeAll(Iterable objects, [String separator = ""]) → void
  Iterates over the given `objects` and writes them in sequence.
  inherited
writeCharCode(int charCode) → void
  Writes the character of `charCode`.
  inherited
writeln([Object? object = ""]) → void
  Converts `object` to a String by invoking Object.toString and writes the result to `this`, followed by a newline.
  inherited

## Operators

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