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 /// And then run `pub install`. |
| 16 /// |
7 /// The easiest way to use this library is via the top-level functions. They | 17 /// 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: | 18 /// allow you to make individual HTTP requests with minimal hassle: |
9 /// | 19 /// |
10 /// import 'package:http/http.dart' as http; | 20 /// import 'package:http/http.dart' as http; |
11 /// | 21 /// |
12 /// var url = "http://example.com/whatsit/create"; | 22 /// var url = "http://example.com/whatsit/create"; |
13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) | 23 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) |
14 /// .then((response) { | 24 /// .then((response) { |
15 /// print("Response status: ${response.statusCode}"); | 25 /// print("Response status: ${response.statusCode}"); |
16 /// print("Response body: ${response.body}"); | 26 /// print("Response body: ${response.body}"); |
(...skipping 26 matching lines...) Expand all Loading... |
43 /// final String userAgent; | 53 /// final String userAgent; |
44 /// final http.Client _inner; | 54 /// final http.Client _inner; |
45 /// | 55 /// |
46 /// UserAgentClient(this.userAgent, this._inner); | 56 /// UserAgentClient(this.userAgent, this._inner); |
47 /// | 57 /// |
48 /// Future<StreamedResponse> send(BaseRequest request) { | 58 /// Future<StreamedResponse> send(BaseRequest request) { |
49 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; | 59 /// request.headers[HttpHeaders.USER_AGENT] = userAgent; |
50 /// return _inner.send(request); | 60 /// return _inner.send(request); |
51 /// } | 61 /// } |
52 /// } | 62 /// } |
| 63 /// |
| 64 /// [pub]: http://pub.dartlang.org |
53 library http; | 65 library http; |
54 | 66 |
55 import 'dart:async'; | 67 import 'dart:async'; |
56 import 'dart:typeddata'; | 68 import 'dart:typeddata'; |
57 import 'dart:uri'; | 69 import 'dart:uri'; |
58 | 70 |
59 import 'src/client.dart'; | 71 import 'src/client.dart'; |
60 import 'src/response.dart'; | 72 import 'src/response.dart'; |
61 | 73 |
62 export 'src/base_client.dart'; | 74 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] | 176 /// For more fine-grained control over the request and response, use [Request] |
165 /// instead. | 177 /// instead. |
166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 178 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
167 _withClient((client) => client.readBytes(url, headers: headers)); | 179 _withClient((client) => client.readBytes(url, headers: headers)); |
168 | 180 |
169 Future _withClient(Future fn(Client)) { | 181 Future _withClient(Future fn(Client)) { |
170 var client = new Client(); | 182 var client = new Client(); |
171 var future = fn(client); | 183 var future = fn(client); |
172 return future.whenComplete(client.close); | 184 return future.whenComplete(client.close); |
173 } | 185 } |
OLD | NEW |