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 /// | |
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 /// | |
20 /// The easiest way to use this library is via the top-level functions. They | |
21 /// allow you to make individual HTTP requests with minimal hassle: | |
22 /// | |
23 /// import 'package:http/http.dart' as http; | |
24 /// | |
25 /// var url = "http://example.com/whatsit/create"; | |
26 /// http.post(url, body: {"name": "doodle", "color": "blue"}) | |
27 /// .then((response) { | |
28 /// print("Response status: ${response.statusCode}"); | |
29 /// print("Response body: ${response.body}"); | |
30 /// }); | |
31 /// | |
32 /// http.read("http://example.com/foobar.txt").then(print); | |
33 /// | |
34 /// If you're making multiple requests to the same server, you can keep open a | |
35 /// persistent connection by using a [Client] rather than making one-off | |
36 /// requests. If you do this, make sure to close the client when you're done: | |
37 /// | |
38 /// var client = new http.Client(); | |
39 /// client.post( | |
40 /// "http://example.com/whatsit/create", | |
41 /// body: {"name": "doodle", "color": "blue"}) | |
42 /// .then((response) => client.get(response.bodyFields['uri'])) | |
43 /// .then((response) => print(response.body)) | |
44 /// .whenComplete(client.close); | |
45 /// | |
46 /// You can also exert more fine-grained control over your requests and | |
47 /// responses by creating [Request] or [StreamedRequest] objects yourself and | |
48 /// passing them to [Client.send]. | |
49 /// | |
50 /// This package is designed to be composable. This makes it easy for external | |
51 /// libraries to work with one another to add behavior to it. Libraries wishing | |
52 /// to add behavior should create a subclass of [BaseClient] that wraps another | |
53 /// [Client] and adds the desired behavior: | |
54 /// | |
55 /// class UserAgentClient extends http.BaseClient { | |
56 /// final String userAgent; | |
57 /// final http.Client _inner; | |
58 /// | |
59 /// UserAgentClient(this.userAgent, this._inner); | |
60 /// | |
61 /// Future<StreamedResponse> send(BaseRequest request) { | |
62 /// request.headers['user-agent'] = userAgent; | |
63 /// return _inner.send(request); | |
64 /// } | |
65 /// } | |
66 /// | |
67 /// [pub]: http://pub.dartlang.org | |
68 library http; | 6 library http; |
69 | 7 |
70 import 'dart:async'; | 8 import 'dart:async'; |
71 import 'dart:convert'; | 9 import 'dart:convert'; |
72 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
73 | 11 |
74 import 'src/client.dart'; | 12 import 'src/client.dart'; |
75 import 'src/response.dart'; | 13 import 'src/response.dart'; |
76 | 14 |
77 export 'src/base_client.dart'; | 15 export 'src/base_client.dart'; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 /// For more fine-grained control over the request and response, use [Request] | 136 /// For more fine-grained control over the request and response, use [Request] |
199 /// instead. | 137 /// instead. |
200 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => | 138 Future<Uint8List> readBytes(url, {Map<String, String> headers}) => |
201 _withClient((client) => client.readBytes(url, headers: headers)); | 139 _withClient((client) => client.readBytes(url, headers: headers)); |
202 | 140 |
203 Future _withClient(Future fn(Client)) { | 141 Future _withClient(Future fn(Client)) { |
204 var client = new Client(); | 142 var client = new Client(); |
205 var future = fn(client); | 143 var future = fn(client); |
206 return future.whenComplete(client.close); | 144 return future.whenComplete(client.close); |
207 } | 145 } |
OLD | NEW |