Dart DocumentationrequestRequest

Request class

An HTTP request where the entire request body is known in advance.

class Request extends BaseRequest {
 /// The size of the request body, in bytes. This is calculated from
 /// [bodyBytes].
 ///
 /// The content length cannot be set for [Request], since it's automatically
 /// calculated from [bodyBytes].
 int get contentLength => bodyBytes.length;

 set contentLength(int value) {
   throw new UnsupportedError("Cannot set the contentLength property of "
       "non-streaming Request objects.");
 }

 /// The default encoding to use when converting between [bodyBytes] and
 /// [body]. This is only used if [encoding] hasn't been manually set and if
 /// the content-type header has no encoding information.
 Encoding _defaultEncoding;

 /// The encoding used for the request. This encoding is used when converting
 /// between [bodyBytes] and [body].
 ///
 /// If the request has a `Content-Type` header and that header has a `charset`
 /// parameter, that parameter's value is used as the encoding. Otherwise, if
 /// [encoding] has been set manually, that encoding is used. If that hasn't
 /// been set either, this defaults to [Encoding.UTF_8].
 ///
 /// If the `charset` parameter's value is not a known [Encoding], reading this
 /// will throw a [FormatException].
 ///
 /// If the request has a `Content-Type` header, setting this will set the
 /// charset parameter on that header.
 Encoding get encoding {
   if (_contentType == null || _contentType.charset == null) {
     return _defaultEncoding;
   }
   return requiredEncodingForCharset(_contentType.charset);
 }

 set encoding(Encoding value) {
   _checkFinalized();
   _defaultEncoding = value;
   var contentType = _contentType;
   if (contentType != null) {
     contentType = new ContentType(contentType.primaryType,
                                   contentType.subType,
                                   charset: value.name,
                                   parameters: contentType.parameters);
     _contentType = contentType;
   }
 }

 // TODO(nweiz): make this return a read-only view
 /// The bytes comprising the body of the request. This is converted to and
 /// from [body] using [encoding].
 ///
 /// This list should only be set, not be modified in place.
 Uint8List get bodyBytes => _bodyBytes;
 Uint8List _bodyBytes;

 set bodyBytes(List<int> value) {
   _checkFinalized();
   _bodyBytes = toUint8List(value);
 }

 /// The body of the request as a string. This is converted to and from
 /// [bodyBytes] using [encoding].
 ///
 /// When this is set, if the request does not yet have a `Content-Type`
 /// header, one will be added with the type `text/plain`. Then the `charset`
 /// parameter of the `Content-Type` header (whether new or pre-existing) will
 /// be set to [encoding] if it wasn't already set.
 String get body => decodeString(bodyBytes, encoding);

 set body(String value) {
   bodyBytes = encodeString(value, encoding);
   var contentType = _contentType;
   if (contentType == null) {
     contentType = new ContentType("text", "plain", charset: encoding.name);
   } else if (contentType.charset == null) {
     contentType = new ContentType(contentType.primaryType,
                                   contentType.subType,
                                   charset: encoding.name,
                                   parameters: contentType.parameters);
   }
   _contentType = contentType;
 }

 /// The form-encoded fields in the body of the request as a map from field
 /// names to values. The form-encoded body is converted to and from
 /// [bodyBytes] using [encoding] (in the same way as [body]).
 ///
 /// If the request doesn't have a `Content-Type` header of
 /// `application/x-www-form-urlencoded`, reading this will throw a
 /// [StateError].
 ///
 /// If the request has a `Content-Type` header with a type other than
 /// `application/x-www-form-urlencoded`, setting this will throw a
 /// [StateError]. Otherwise, the content type will be set to
 /// `application/x-www-form-urlencoded`.
 ///
 /// This map should only be set, not modified in place.
 Map<String, String> get bodyFields {
   if (_contentType == null ||
       _contentType.value != "application/x-www-form-urlencoded") {
     throw new StateError('Cannot access the body fields of a Request without '
         'content-type "application/x-www-form-urlencoded".');
   }

   return queryToMap(body);
 }

 set bodyFields(Map<String, String> fields) {
   if (_contentType == null) {
     _contentType = new ContentType("application", "x-www-form-urlencoded");
   } else if (_contentType.value != "application/x-www-form-urlencoded") {
     throw new StateError('Cannot set the body fields of a Request with '
         'content-type "${_contentType.value}".');
   }

   this.body = mapToQuery(fields);
 }

 /// Creates a new HTTP request.
 Request(String method, Uri url)
   : super(method, url),
     _defaultEncoding = Encoding.UTF_8,
     _bodyBytes = new Uint8List(0);

 /// Freezes all mutable fields and returns a single-subscription [ByteStream]
 /// containing the request body.
 ByteStream finalize() {
   super.finalize();
   return new ByteStream.fromBytes(bodyBytes);
 }

 /// The `Content-Type` header of the request (if it exists) as a
 /// [ContentType].
 ContentType get _contentType {
   var contentType = headers[HttpHeaders.CONTENT_TYPE];
   if (contentType == null) return null;
   return ContentType.parse(contentType);
 }

 set _contentType(ContentType value) {
   headers[HttpHeaders.CONTENT_TYPE] = value.toString();
 }

 /// Throw an error if this request has been finalized.
 void _checkFinalized() {
   if (!finalized) return;
   throw new StateError("Can't modify a finalized Request.");
 }
}

Extends

BaseRequest > Request

Constructors

new Request(String method, Uri url) #

Creates a new HTTP request.

Request(String method, Uri url)
 : super(method, url),
   _defaultEncoding = Encoding.UTF_8,
   _bodyBytes = new Uint8List(0);

Properties

String body #

The body of the request as a string. This is converted to and from bodyBytes using encoding.

When this is set, if the request does not yet have a Content-Type header, one will be added with the type text/plain. Then the charset parameter of the Content-Type header (whether new or pre-existing) will be set to encoding if it wasn't already set.

String get body => decodeString(bodyBytes, encoding);
set body(String value) {
 bodyBytes = encodeString(value, encoding);
 var contentType = _contentType;
 if (contentType == null) {
   contentType = new ContentType("text", "plain", charset: encoding.name);
 } else if (contentType.charset == null) {
   contentType = new ContentType(contentType.primaryType,
                                 contentType.subType,
                                 charset: encoding.name,
                                 parameters: contentType.parameters);
 }
 _contentType = contentType;
}

Uint8List bodyBytes #

The bytes comprising the body of the request. This is converted to and from body using encoding.

This list should only be set, not be modified in place.

Uint8List get bodyBytes => _bodyBytes;
set bodyBytes(List<int> value) {
 _checkFinalized();
 _bodyBytes = toUint8List(value);
}

Map<String, String> bodyFields #

The form-encoded fields in the body of the request as a map from field names to values. The form-encoded body is converted to and from bodyBytes using encoding (in the same way as body).

If the request doesn't have a Content-Type header of application/x-www-form-urlencoded, reading this will throw a StateError.

If the request has a Content-Type header with a type other than application/x-www-form-urlencoded, setting this will throw a StateError. Otherwise, the content type will be set to application/x-www-form-urlencoded.

This map should only be set, not modified in place.

Map<String, String> get bodyFields {
 if (_contentType == null ||
     _contentType.value != "application/x-www-form-urlencoded") {
   throw new StateError('Cannot access the body fields of a Request without '
       'content-type "application/x-www-form-urlencoded".');
 }

 return queryToMap(body);
}
set bodyFields(Map<String, String> fields) {
 if (_contentType == null) {
   _contentType = new ContentType("application", "x-www-form-urlencoded");
 } else if (_contentType.value != "application/x-www-form-urlencoded") {
   throw new StateError('Cannot set the body fields of a Request with '
       'content-type "${_contentType.value}".');
 }

 this.body = mapToQuery(fields);
}

int contentLength #

The size of the request body, in bytes. This is calculated from bodyBytes.

The content length cannot be set for Request, since it's automatically calculated from bodyBytes.

int get contentLength => bodyBytes.length;
set contentLength(int value) {
 throw new UnsupportedError("Cannot set the contentLength property of "
     "non-streaming Request objects.");
}

Encoding encoding #

The encoding used for the request. This encoding is used when converting between bodyBytes and body.

If the request has a Content-Type header and that header has a charset parameter, that parameter's value is used as the encoding. Otherwise, if encoding has been set manually, that encoding is used. If that hasn't been set either, this defaults to Encoding.UTF_8.

If the charset parameter's value is not a known Encoding, reading this will throw a FormatException.

If the request has a Content-Type header, setting this will set the charset parameter on that header.

Encoding get encoding {
 if (_contentType == null || _contentType.charset == null) {
   return _defaultEncoding;
 }
 return requiredEncodingForCharset(_contentType.charset);
}
set encoding(Encoding value) {
 _checkFinalized();
 _defaultEncoding = value;
 var contentType = _contentType;
 if (contentType != null) {
   contentType = new ContentType(contentType.primaryType,
                                 contentType.subType,
                                 charset: value.name,
                                 parameters: contentType.parameters);
   _contentType = contentType;
 }
}

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 Uri url #

inherited from BaseRequest

The URL to which the request will be sent.

final Uri url

Methods

ByteStream finalize() #

Freezes all mutable fields and returns a single-subscription ByteStream containing the request body.

ByteStream finalize() {
 super.finalize();
 return new ByteStream.fromBytes(bodyBytes);
}

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";