OLD | NEW |
1 # http | 1 # http |
2 | 2 |
3 A composable, Future-based library for making HTTP requests. | 3 A composable, Future-based library for making HTTP requests. |
4 | 4 |
5 This package contains a set of high-level functions and classes that make it | 5 This package contains a set of high-level functions and classes that make it |
6 easy to consume HTTP resources. | 6 easy to consume HTTP resources. |
7 | 7 |
8 **NOTE:** This package currently only works for | 8 **NOTE:** This package currently only works for |
9 server-side or command-line Dart applications. In other words, if the app | 9 server-side or command-line Dart applications. In other words, if the app |
10 imports `dart:io`, it can use this package. | 10 imports `dart:io`, it can use this package. |
11 | 11 |
12 ## Using | 12 ## Using |
13 | 13 |
14 Please see the [API docs][docs] for explanations and examples. | 14 The easiest way to use this library is via the top-level functions. They allow |
| 15 you to make individual HTTP requests with minimal hassle: |
| 16 |
| 17 ```dart |
| 18 import 'package:http/http.dart' as http; |
| 19 |
| 20 var url = "http://example.com/whatsit/create"; |
| 21 http.post(url, body: {"name": "doodle", "color": "blue"}) |
| 22 .then((response) { |
| 23 print("Response status: ${response.statusCode}"); |
| 24 print("Response body: ${response.body}"); |
| 25 }); |
| 26 |
| 27 http.read("http://example.com/foobar.txt").then(print); |
| 28 ``` |
| 29 |
| 30 If you're making multiple requests to the same server, you can keep open a |
| 31 persistent connection by using a [Client][] rather than making one-off requests. |
| 32 If you do this, make sure to close the client when you're done: |
| 33 |
| 34 ```dart |
| 35 var client = new http.Client(); |
| 36 client.post( |
| 37 "http://example.com/whatsit/create", |
| 38 body: {"name": "doodle", "color": "blue"}) |
| 39 .then((response) => client.get(response.bodyFields['uri'])) |
| 40 .then((response) => print(response.body)) |
| 41 .whenComplete(client.close); |
| 42 ``` |
| 43 |
| 44 You can also exert more fine-grained control over your requests and responses by |
| 45 creating [Request][] or [StreamedRequest][] objects yourself and passing them to |
| 46 [Client.send][]. |
| 47 |
| 48 [Request]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/http/
http.Request |
| 49 |
| 50 [StreamedRequest]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-view
er/http/http.StreamedRequest |
| 51 |
| 52 [Client.send]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/h
ttp/http.Client#id_send |
| 53 |
| 54 This package is designed to be composable. This makes it easy for external |
| 55 libraries to work with one another to add behavior to it. Libraries wishing to |
| 56 add behavior should create a subclass of [BaseClient][] that wraps another |
| 57 [Client][] and adds the desired behavior: |
| 58 |
| 59 [BaseClient]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/ht
tp/http.BaseClient |
| 60 |
| 61 [Client]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/http/h
ttp.Client |
| 62 |
| 63 ```dart |
| 64 class UserAgentClient extends http.BaseClient { |
| 65 final String userAgent; |
| 66 final http.Client _inner; |
| 67 |
| 68 UserAgentClient(this.userAgent, this._inner); |
| 69 |
| 70 Future<StreamedResponse> send(BaseRequest request) { |
| 71 request.headers['user-agent'] = userAgent; |
| 72 return _inner.send(request); |
| 73 } |
| 74 } |
| 75 ``` |
15 | 76 |
16 ## Filing issues | 77 ## Filing issues |
17 | 78 |
18 Please file issues for the http package at [http://dartbug.com/new][bugs]. | 79 Please file issues for the http package at [http://dartbug.com/new][bugs]. |
19 | 80 |
20 [bugs]: http://dartbug.com/new | 81 [bugs]: http://dartbug.com/new |
21 [docs]: https://api.dartlang.org/docs/channels/dev/latest/http.html | 82 [docs]: https://api.dartlang.org/docs/channels/dev/latest/http.html |
OLD | NEW |