W3cubDocs

/Dart 2

HttpResponse class

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.

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
    = new 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.

An exception is thrown if you use the write() method while an unsupported content-type is set.

Implemented types

Constructors

HttpResponse()

Properties

bufferOutputbool
read / write
Gets or sets if the HttpResponse should buffer output. [...]
connectionInfoHttpConnectionInfo
read-only
Gets information about the client connection. Returns null if the socket is not available.
contentLengthint
read / write
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.
cookiesList<Cookie>
read-only
Cookies to set in the client (in the 'set-cookie' header).
deadlineDuration
read / write
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. [...]
headersHttpHeaders
read-only
Returns the response headers. [...]
persistentConnectionbool
read / write
Gets and sets the persistent connection state. The initial value of this property is the persistent connection state from the request.
reasonPhraseString
read / write
Gets and sets the reason phrase. If no reason phrase is explicitly set a default reason phrase is provided. [...]
statusCodeint
read / write
Gets and sets the status code. Any integer value is accepted. For the official HTTP status codes use the fields from HttpStatus. If no status code is explicitly set the default value HttpStatus.ok is used. [...]
doneFuture
read-only, inherited
Get a future that will complete when the consumer closes, or when an error occurs. This future is identical to the future returned by close.
encodingEncoding
read / write, inherited
The Encoding used when writing strings. Depending on the underlying consumer this property might be mutable.
hashCodeint
read-only, inherited
The hash code for this object. [...]
runtimeTypeType
read-only, inherited
A representation of the runtime type of the object.

Methods

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. [...]
redirect(Uri location, { int status: HttpStatus.movedTemporarily }) → Future
Respond with a redirect to location. [...]
add(List<int> data) → void
inherited
Adds byte data to the target consumer, ignoring encoding. [...]
addError(Object error, [ StackTrace stackTrace ]) → void
inherited
Passes the error to the target consumer as an error event. [...]
addStream(Stream<List<int>> stream) → Future
inherited
Adds all elements of the given stream to this. [...]
close() → Future
inherited
Close the target consumer. [...]
flush() → Future
inherited
Returns a Future that completes once all buffered data is accepted by the underlying StreamConsumer. [...]
noSuchMethod(Invocation invocation) → dynamic
inherited
Invoked when a non-existent method or property is accessed. [...]
toString() → String
inherited
Returns a string representation of this object.
write(Object obj) → void
inherited
Converts obj to a String by invoking Object.toString and adds the encoding of the result to the target consumer. [...]
writeAll(Iterable objects, [ String separator = "" ]) → void
inherited
Iterates over the given objects and writes them in sequence. [...]
writeCharCode(int charCode) → void
inherited
Writes the character of charCode. [...]
writeln([Object obj = "" ]) → void
inherited
Converts obj to a String by invoking Object.toString and writes the result to this, followed by a newline. [...]

Operators

operator ==(dynamic other) → bool
inherited
The equality operator. [...]

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-io/HttpResponse-class.html