OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// A composable, [Future]-based library for making HTTP requests. | 5 /// A composable, [Future]-based library for making HTTP requests. |
6 /// | 6 /// |
| 7 /// ## Installing ## |
| 8 /// |
| 9 /// Use [pub][] to install this package. Add the following to your |
| 10 /// `pubspec.yaml` file. |
| 11 /// |
| 12 /// dependencies: |
| 13 /// http: any |
| 14 /// |
| 15 /// Then run `pub install`. |
| 16 /// |
| 17 /// For more information, see the |
| 18 /// [http package on pub.dartlang.org](http://pub.dartlang.org/packages/http). |
| 19 /// |
7 /// The easiest way to use this library is via the top-level functions. They | 20 /// The easiest way to use this library is via the top-level functions. They |
8 /// allow you to make individual HTTP requests with minimal hassle: | 21 /// allow you to make individual HTTP requests with minimal hassle: |
9 /// | 22 /// |
10 /// import 'package:http/http.dart' as http; | 23 /// import 'package:http/http.dart' as http; |
11 /// | 24 /// |
12 /// var url = "http://example.com/whatsit/create"; | 25 /// var url = "http://example.com/whatsit/create"; |
13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) | 26 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) |
14 /// .then((response) { | 27 /// .then((response) { |
15 /// print("Response status: ${response.statusCode}"); | 28 /// print("Response status: ${response.statusCode}"); |
16 /// print("Response body: ${response.body}"); | 29 /// print("Response body: ${response.body}"); |
(...skipping 26 matching lines...) Expand all Loading... |
43 /// final String userAgent; | 56 /// final String userAgent; |
44 /// final http.Client _inner; | 57 /// final http.Client _inner; |
45 /// | 58 /// |
46 /// UserAgentClient(this.userAgent, this._inner); | 59 /// UserAgentClient(this.userAgent, this._inner); |
47 /// | 60 /// |
48 /// Future<StreamedResponse> send(BaseRequest request) { | 61 /// Future<StreamedResponse> send(BaseRequest request) { |
49 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; | 62 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; |
50 /// return _inner.send(request); | 63 /// return _inner.send(request); |
51 /// } | 64 /// } |
52 /// } | 65 /// } |
| 66 /// |
| 67 /// [pub]: http://pub.dartlang.org |
53 library http; | 68 library http; |
54 | 69 |
55 import 'dart:async'; | 70 import 'dart:async'; |
56 import 'dart:typeddata'; | 71 import 'dart:typeddata'; |
57 import 'dart:uri'; | 72 import 'dart:uri'; |
58 | 73 |
59 import 'src/client.dart'; | 74 import 'src/client.dart'; |
60 import 'src/response.dart'; | 75 import 'src/response.dart'; |
61 | 76 |
62 export 'src/base_client.dart'; | 77 export 'src/base_client.dart'; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 /// For more fine-grained control over the request and response, use [Request] | 179 /// For more fine-grained control over the request and response, use [Request] |
165 /// instead. | 180 /// instead. |
166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 181 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
167 _withClient((client) => client.readBytes(url, headers: headers)); | 182 _withClient((client) => client.readBytes(url, headers: headers)); |
168 | 183 |
169 Future _withClient(Future fn(Client)) { | 184 Future _withClient(Future fn(Client)) { |
170 var client = new Client(); | 185 var client = new Client(); |
171 var future = fn(client); | 186 var future = fn(client); |
172 return future.whenComplete(client.close); | 187 return future.whenComplete(client.close); |
173 } | 188 } |
OLD | NEW |