Dart Documentationstreamed_requestStreamedRequest

StreamedRequest class

An HTTP request where the request body is sent asynchronously after the connection has been established and the headers have been sent.

When the request is sent via BaseClient.send, only the headers and whatever data has already been written to StreamedRequest.stream will be sent immediately. More data will be sent as soon as it's written to StreamedRequest.sink, and when the sink is closed the request will end.

class StreamedRequest extends BaseRequest {
 /// The sink to which to write data that will be sent as the request body.
 /// This may be safely written to before the request is sent; the data will be
 /// buffered.
 ///
 /// Closing this signals the end of the request.
 EventSink<List<int>> get sink => _controller.sink;

 /// The controller for [sink], from which [BaseRequest] will read data for
 /// [finalize].
 final StreamController<List<int>> _controller;

 /// Creates a new streaming request.
 StreamedRequest(String method, Uri url)
   : super(method, url),
     _controller = new StreamController<List<int>>(sync: true);

 /// Freezes all mutable fields other than [stream] and returns a
 /// single-subscription [ByteStream] that emits the data being written to
 /// [sink].
 ByteStream finalize() {
   super.finalize();
   return new ByteStream(_controller.stream);
 }
}

Extends

BaseRequest > StreamedRequest

Constructors

new StreamedRequest(String method, Uri url) #

Creates a new streaming request.

StreamedRequest(String method, Uri url)
 : super(method, url),
   _controller = new StreamController<List<int>>(sync: true);

Properties

int contentLength #

inherited from BaseRequest

The size of the request body, in bytes. This defaults to -1, which indicates that the size of the request is not known in advance.

int get contentLength => _contentLength;
set contentLength(int value) {
 _checkFinalized();
 _contentLength = value;
}

final bool finalized #

inherited from BaseRequest

Whether the request has been finalized.

bool get finalized => _finalized;

bool followRedirects #

inherited from BaseRequest

Whether the client should follow redirects while resolving this request. Defaults to true.

bool get followRedirects => _followRedirects;
set followRedirects(bool value) {
 _checkFinalized();
 _followRedirects = value;
}

final Map<String, String> headers #

inherited from BaseRequest

The headers for this request.

final Map<String, String> headers

int maxRedirects #

inherited from BaseRequest

The maximum number of redirects to follow when followRedirects is true. If this number is exceeded the BaseResponse future will signal a RedirectException. Defaults to 5.

int get maxRedirects => _maxRedirects;
set maxRedirects(int value) {
 _checkFinalized();
 _maxRedirects = value;
}

final String method #

inherited from BaseRequest

The HTTP method of the request. Most commonly "GET" or "POST", less commonly "HEAD", "PUT", or "DELETE". Non-standard method names are also supported.

final String method

bool persistentConnection #

inherited from BaseRequest

Whether a persistent connection should be maintained with the server. Defaults to true.

bool get persistentConnection => _persistentConnection;
set persistentConnection(bool value) {
 _checkFinalized();
 _persistentConnection = value;
}

final EventSink<List<int>> sink #

The sink to which to write data that will be sent as the request body. This may be safely written to before the request is sent; the data will be buffered.

Closing this signals the end of the request.

EventSink<List<int>> get sink => _controller.sink;

final Uri url #

inherited from BaseRequest

The URL to which the request will be sent.

final Uri url

Methods

ByteStream finalize() #

Freezes all mutable fields other than stream and returns a single-subscription ByteStream that emits the data being written to sink.

ByteStream finalize() {
 super.finalize();
 return new ByteStream(_controller.stream);
}

Future<StreamedResponse> send() #

inherited from BaseRequest

Sends this request.

This automatically initializes a new Client and closes that client once the request is complete. If you're planning on making multiple requests to the same server, you should use a single Client for all of those requests.

Future<StreamedResponse> send() {
 var client = new Client();
 return client.send(this).then((response) {
   var stream = onDone(response.stream, client.close);
   return new StreamedResponse(
       new ByteStream(stream),
       response.statusCode,
       response.contentLength,
       request: response.request,
       headers: response.headers,
       isRedirect: response.isRedirect,
       persistentConnection: response.persistentConnection,
       reasonPhrase: response.reasonPhrase);
 }).catchError((e) {
   client.close();
   throw e;
 });
}

String toString() #

inherited from BaseRequest

Returns a string representation of this object.

docs inherited from Object
String toString() => "$method $url";