| OLD | NEW |
| 1 // Copyright (c) 2012, 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 /// The easiest way to use this library is via the top-level functions. They | 7 /// 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: | 8 /// allow you to make individual HTTP requests with minimal hassle: |
| 9 /// | 9 /// |
| 10 /// import 'package:http/http.dart' as http; | 10 /// import 'package:http/http.dart' as http; |
| 11 /// | 11 /// |
| 12 /// var url = "http://example.com/whatsit/create"; | 12 /// var url = "http://example.com/whatsit/create"; |
| 13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) | 13 /// http.post(url, fields: {"name": "doodle", "color": "blue"}) |
| 14 /// .then((response) { | 14 /// .then((response) { |
| 15 /// print("Response status: ${response.statusCode}"); | 15 /// print("Response status: ${response.statusCode}"); |
| 16 /// print("Response body: ${response.body}"); | 16 /// print("Response body: ${response.body}"); |
| 17 /// }); | 17 /// }); |
| 18 /// | 18 /// |
| 19 /// http.read("http://example.com/foobar.txt").then(print); | 19 /// http.read("http://example.com/foobar.txt").then(print); |
| 20 /// | 20 /// |
| 21 /// If you're making multiple requests to the same server, you can keep open a | 21 /// If you're making multiple requests to the same server, you can keep open a |
| 22 /// persistent connection by using a [Client] rather than making one-off | 22 /// persistent connection by using a [Client] rather than making one-off |
| 23 /// requests. If you do this, make sure to close the client when you're done: | 23 /// requests. If you do this, make sure to close the client when you're done: |
| 24 /// | 24 /// |
| 25 /// var client = new http.Client(); | 25 /// var client = new http.Client(); |
| 26 /// client.post( | 26 /// client.post( |
| 27 /// "http://example.com/whatsit/create", | 27 /// "http://example.com/whatsit/create", |
| 28 /// fields: {"name": "doodle", "color": "blue"}) | 28 /// fields: {"name": "doodle", "color": "blue"}) |
| 29 /// .chain((response) => client.get(response.bodyFields['uri'])) | 29 /// .then((response) => client.get(response.bodyFields['uri'])) |
| 30 /// .then((response) => print(response.body)) | 30 /// .then((response) => print(response.body)) |
| 31 /// .onComplete((_) => client.close()); | 31 /// .onComplete((_) => client.close()); |
| 32 /// | 32 /// |
| 33 /// You can also exert more fine-grained control over your requests and | 33 /// You can also exert more fine-grained control over your requests and |
| 34 /// responses by creating [Request] or [StreamedRequest] objects yourself and | 34 /// responses by creating [Request] or [StreamedRequest] objects yourself and |
| 35 /// passing them to [Client.send]. | 35 /// passing them to [Client.send]. |
| 36 /// | 36 /// |
| 37 /// This package is designed to be composable. This makes it easy for external | 37 /// This package is designed to be composable. This makes it easy for external |
| 38 /// libraries to work with one another to add behavior to it. Libraries wishing | 38 /// libraries to work with one another to add behavior to it. Libraries wishing |
| 39 /// to add behavior should create a subclass of [BaseClient] that wraps another | 39 /// to add behavior should create a subclass of [BaseClient] that wraps another |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 /// instead. | 165 /// instead. |
| 166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 166 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
| 167 _withClient((client) => client.readBytes(url, headers: headers)); | 167 _withClient((client) => client.readBytes(url, headers: headers)); |
| 168 | 168 |
| 169 Future _withClient(Future fn(Client)) { | 169 Future _withClient(Future fn(Client)) { |
| 170 var client = new Client(); | 170 var client = new Client(); |
| 171 var future = fn(client); | 171 var future = fn(client); |
| 172 future.catchError((_) {}).then((_) => client.close()); | 172 future.catchError((_) {}).then((_) => client.close()); |
| 173 return future; | 173 return future; |
| 174 } | 174 } |
| OLD | NEW |