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