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.
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.
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.
HttpResponse
should buffer output. [...] null
if the socket is not available. 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. [...] HttpStatus
. If no status code is explicitly set the default value HttpStatus.ok
is used. [...] Encoding
used when writing strings. Depending on the underlying consumer this property might be mutable. location
. [...] data
to the target consumer, ignoring encoding. [...] stream
to this
. [...] obj
to a String by invoking Object.toString and adds the encoding of the result to the target consumer. [...] objects
and writes them in sequence. [...] charCode
. [...] obj
to a String by invoking Object.toString and writes the result to this
, followed by a newline. [...]
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-io/HttpResponse-class.html