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 The easiest way to use this library is via the top-level functions. They allow | 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 |
15 you to make individual HTTP requests with minimal hassle: | 14 you to make individual HTTP requests with minimal hassle: |
16 | 15 |
17 ```dart | 16 ```dart |
18 import 'package:http/http.dart' as http; | 17 import 'package:http/http.dart' as http; |
19 | 18 |
20 var url = "http://example.com/whatsit/create"; | 19 var url = "http://example.com/whatsit/create"; |
21 http.post(url, body: {"name": "doodle", "color": "blue"}) | 20 http.post(url, body: {"name": "doodle", "color": "blue"}) |
22 .then((response) { | 21 .then((response) { |
23 print("Response status: ${response.statusCode}"); | 22 print("Response status: ${response.statusCode}"); |
24 print("Response body: ${response.body}"); | 23 print("Response body: ${response.body}"); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 66 |
68 UserAgentClient(this.userAgent, this._inner); | 67 UserAgentClient(this.userAgent, this._inner); |
69 | 68 |
70 Future<StreamedResponse> send(BaseRequest request) { | 69 Future<StreamedResponse> send(BaseRequest request) { |
71 request.headers['user-agent'] = userAgent; | 70 request.headers['user-agent'] = userAgent; |
72 return _inner.send(request); | 71 return _inner.send(request); |
73 } | 72 } |
74 } | 73 } |
75 ``` | 74 ``` |
76 | 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 ``` |
| 94 |
77 ## Filing issues | 95 ## Filing issues |
78 | 96 |
79 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]. |
80 | 98 |
81 [bugs]: http://dartbug.com/new | 99 [bugs]: http://dartbug.com/new |
82 [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 |