Skip to content

Commit bccc9ab

Browse files
authored
Fix syntax error in MultipartRequest code example (#302)
Fixes #164 The primary syntax bug is a missing closing paren. This commit also fixes some style: - Use cascades in the sample code. - Use consistent single quotes in the sample code. - Use a blank line following the first sentence of all doc comments. - Delete some wholly redundant doc comments. - Rename a private constant to lower camel case.
1 parent 296733e commit bccc9ab

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.0+3
2+
3+
* Documentation fixes.
4+
15
## 0.12.0+2
26

37
* Documentation fixes.

lib/src/multipart_request.dart

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,51 @@ import 'utils.dart';
1414

1515
final _newlineRegExp = RegExp(r"\r\n|\r|\n");
1616

17-
/// A `multipart/form-data` request. Such a request has both string [fields],
18-
/// which function as normal form fields, and (potentially streamed) binary
19-
/// [files].
17+
/// A `multipart/form-data` request.
18+
///
19+
/// Such a request has both string [fields], which function as normal form
20+
/// fields, and (potentially streamed) binary [files].
2021
///
2122
/// This request automatically sets the Content-Type header to
2223
/// `multipart/form-data`. This value will override any value set by the user.
2324
///
24-
/// var uri = Uri.parse("https://example.com/create");
25-
/// var request = new http.MultipartRequest("POST", uri);
26-
/// request.fields['user'] = '[email protected]';
27-
/// request.files.add(new http.MultipartFile.fromPath(
28-
/// 'package',
29-
/// 'build/package.tar.gz',
30-
/// contentType: new MediaType('application', 'x-tar'));
25+
/// var uri = Uri.parse('https://example.com/create');
26+
/// var request = http.MultipartRequest('POST', uri)
27+
/// ..fields['user'] = '[email protected]'
28+
/// ..files.add(http.MultipartFile.fromPath(
29+
/// 'package', 'build/package.tar.gz',
30+
/// contentType: MediaType('application', 'x-tar')));
3131
/// var response = await request.send();
3232
/// if (response.statusCode == 200) print('Uploaded!');
3333
class MultipartRequest extends BaseRequest {
3434
/// The total length of the multipart boundaries used when building the
35-
/// request body. According to http://tools.ietf.org/html/rfc1341.html, this
36-
/// can't be longer than 70.
37-
static const int _BOUNDARY_LENGTH = 70;
35+
/// request body.
36+
///
37+
/// According to http://tools.ietf.org/html/rfc1341.html, this can't be longer
38+
/// than 70.
39+
static const int _boundaryLength = 70;
3840

3941
static final Random _random = Random();
4042

4143
/// The form fields to send for this request.
42-
final Map<String, String> fields;
44+
final fields = <String, String>{};
4345

44-
/// The private version of [files].
45-
final List<MultipartFile> _files;
46+
final _files = <MultipartFile>[];
4647

47-
/// Creates a new [MultipartRequest].
48-
MultipartRequest(String method, Uri url)
49-
: fields = {},
50-
_files = <MultipartFile>[],
51-
super(method, url);
48+
MultipartRequest(String method, Uri url) : super(method, url);
5249

5350
/// The list of files to upload for this request.
5451
List<MultipartFile> get files => _files;
5552

56-
/// The total length of the request body, in bytes. This is calculated from
57-
/// [fields] and [files] and cannot be set manually.
53+
/// The total length of the request body, in bytes.
54+
///
55+
/// This is calculated from [fields] and [files] and cannot be set manually.
5856
int get contentLength {
5957
var length = 0;
6058

6159
fields.forEach((name, value) {
6260
length += "--".length +
63-
_BOUNDARY_LENGTH +
61+
_boundaryLength +
6462
"\r\n".length +
6563
utf8.encode(_headerForField(name, value)).length +
6664
utf8.encode(value).length +
@@ -69,14 +67,14 @@ class MultipartRequest extends BaseRequest {
6967

7068
for (var file in _files) {
7169
length += "--".length +
72-
_BOUNDARY_LENGTH +
70+
_boundaryLength +
7371
"\r\n".length +
7472
utf8.encode(_headerForFile(file)).length +
7573
file.length +
7674
"\r\n".length;
7775
}
7876

79-
return length + "--".length + _BOUNDARY_LENGTH + "--\r\n".length;
77+
return length + "--".length + _boundaryLength + "--\r\n".length;
8078
}
8179

8280
set contentLength(int value) {
@@ -123,8 +121,9 @@ class MultipartRequest extends BaseRequest {
123121
return ByteStream(controller.stream);
124122
}
125123

126-
/// Returns the header string for a field. The return value is guaranteed to
127-
/// contain only ASCII characters.
124+
/// Returns the header string for a field.
125+
///
126+
/// The return value is guaranteed to contain only ASCII characters.
128127
String _headerForField(String name, String value) {
129128
var header =
130129
'content-disposition: form-data; name="${_browserEncode(name)}"';
@@ -136,8 +135,9 @@ class MultipartRequest extends BaseRequest {
136135
return '$header\r\n\r\n';
137136
}
138137

139-
/// Returns the header string for a file. The return value is guaranteed to
140-
/// contain only ASCII characters.
138+
/// Returns the header string for a file.
139+
///
140+
/// The return value is guaranteed to contain only ASCII characters.
141141
String _headerForFile(MultipartFile file) {
142142
var header = 'content-type: ${file.contentType}\r\n'
143143
'content-disposition: form-data; name="${_browserEncode(file.field)}"';
@@ -162,7 +162,7 @@ class MultipartRequest extends BaseRequest {
162162
String _boundaryString() {
163163
var prefix = "dart-http-boundary-";
164164
var list = List<int>.generate(
165-
_BOUNDARY_LENGTH - prefix.length,
165+
_boundaryLength - prefix.length,
166166
(index) =>
167167
BOUNDARY_CHARACTERS[_random.nextInt(BOUNDARY_CHARACTERS.length)],
168168
growable: false);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: http
2-
version: 0.12.0+2
2+
version: 0.12.0+3-dev
33
author: "Dart Team <[email protected]>"
44
homepage: https://github.com/dart-lang/http
55
description: A composable, multi-platform, Future-based API for HTTP requests.

0 commit comments

Comments
 (0)