Dart API Referencedart:ioHttpResponse

HttpResponse abstract class

An HttpResponse represents the headers and data to be returned to a client in response to an HTTP request.

This object has a number of properties for setting up the HTTP header of the response. When the header has been set up the methods from the IOSink can be used to write the actual body of the HTTP response. When one of the IOSink methods is used for the first time the request header is send. Calling any methods that will change the header after it is sent will throw an exception.

When writing string data through the IOSink the encoding used will be 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.CONTENT_TYPE, "text/plain");
response.write(...);  // Strings written will be ISO-8859-1 encoded.

If an unsupported encoding is used an exception will be thrown if using one of the write methods taking a string.

abstract class HttpResponse implements IOSink {
 // TODO(ajohnsen): Add documentation of how to pipe a file to the response.
 /**
  * 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.
  */
 int contentLength;

 /**
  * 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.
  */
 int statusCode;

 /**
  * Gets and sets the reason phrase. If no reason phrase is explicitly
  * set a default reason phrase is provided.
  */
 String reasonPhrase;

 /**
  * Gets and sets the persistent connection state. The initial value
  * of this property is the persistent connection state from the
  * request.
  */
 bool persistentConnection;

 /**
  * Returns the response headers.
  */
 HttpHeaders get headers;

 /**
  * Cookies to set in the client (in the 'set-cookie' header).
  */
 List<Cookie> get cookies;

 /**
  * Detaches the underlying socket from the HTTP server. When the
  * socket is detached the HTTP server will no longer perform any
  * operations on it.
  *
  * This is normally used when a HTTP upgrade request is received
  * and the communication should continue with a different protocol.
  */
 Future<Socket> detachSocket();

 /**
  * Gets information about the client connection. Returns [null] if the socket
  * is not available.
  */
 HttpConnectionInfo get connectionInfo;
}

Implements

IOSink

Properties

final HttpConnectionInfo connectionInfo #

Gets information about the client connection. Returns null if the socket is not available.

HttpConnectionInfo get connectionInfo;

int contentLength #

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.

int contentLength

final List<Cookie> cookies #

Cookies to set in the client (in the 'set-cookie' header).

List<Cookie> get cookies;

final Future done #

inherited from IOSink

Get a future that will complete when all synchronous have completed, or an error happened. This future is identical to the future returned from close.

Future get done;

Encoding encoding #

inherited from IOSink

The Encoding used when writing strings. Depending on the underlying consumer this property might be mutable.

Encoding encoding

final HttpHeaders headers #

Returns the response headers.

HttpHeaders get headers;

bool persistentConnection #

Gets and sets the persistent connection state. The initial value of this property is the persistent connection state from the request.

bool persistentConnection

String reasonPhrase #

Gets and sets the reason phrase. If no reason phrase is explicitly set a default reason phrase is provided.

String reasonPhrase

int statusCode #

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.

int statusCode

Methods

abstract void add(List<int> data) #

inherited from IOSink

Writes the bytes uninterpreted to the consumer.

abstract void addError(error) #

inherited from IOSink

Writes an error to the consumer.

abstract Future addStream(Stream<List<int>> stream) #

inherited from IOSink

Adds all elements of the given stream to this.

abstract Future close() #

inherited from IOSink

Close the target.

abstract Future<Socket> detachSocket() #

Detaches the underlying socket from the HTTP server. When the socket is detached the HTTP server will no longer perform any operations on it.

This is normally used when a HTTP upgrade request is received and the communication should continue with a different protocol.

abstract void write(Object obj) #

inherited from StringSink

Converts obj to a String by invoking toString and adds the result to this.

abstract void writeAll(Iterable objects, [String separator = ""]) #

inherited from StringSink

Iterates over the given objects and writes them in sequence.

abstract void writeCharCode(int charCode) #

inherited from StringSink

Writes the charCode to this.

This method is equivalent to write(new String.fromCharCode(charCode)).

abstract void writeln([Object obj = ""]) #

inherited from StringSink

Converts obj to a String by invoking toString and adds the result to this. Then adds a new line.