@@ -5,26 +5,22 @@ A composable, Future-based library for making HTTP requests.
5
5
6
6
This package contains a set of high-level functions and classes that make it
7
7
easy to consume HTTP resources. It's platform-independent, and can be used on
8
- both the command-line and the browser. Currently the global utility functions
9
- are unsupported on the browser; see "Using on the Browser" below.
8
+ both the command-line and the browser.
10
9
11
10
## Using
12
11
13
- The easiest way to use this library is via the top-level functions, although
14
- they currently only work on platforms where ` dart:io ` is available. They allow
12
+ The easiest way to use this library is via the top-level functions. They allow
15
13
you to make individual HTTP requests with minimal hassle:
16
14
17
15
``` dart
18
16
import 'package:http/http.dart' as http;
19
17
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
- });
18
+ var url = 'http://example.com/whatsit/create';
19
+ var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
20
+ print('Response status: ${response.statusCode}');
21
+ print('Response body: ${response.body}');
26
22
27
- http.read(" http://example.com/foobar.txt").then(print );
23
+ print(await http.read(' http://example.com/foobar.txt') );
28
24
```
29
25
30
26
If you're making multiple requests to the same server, you can keep open a
@@ -33,29 +29,30 @@ If you do this, make sure to close the client when you're done:
33
29
34
30
``` dart
35
31
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);
32
+ try {
33
+ var uriResponse = await client.post('http://example.com/whatsit/create',
34
+ body: {'name': 'doodle', 'color': 'blue'});
35
+ print(await client.get(uriResponse.bodyFields['uri']));
36
+ } finally {
37
+ client.close();
38
+ }
42
39
```
43
40
44
41
You can also exert more fine-grained control over your requests and responses by
45
42
creating [ Request] [ ] or [ StreamedRequest] [ ] objects yourself and passing them to
46
43
[ Client.send] [ ] .
47
44
48
- [ Request ] : https://www.dartdocs .org/documentation/http/latest/http/Request-class.html
49
- [ StreamedRequest ] : https://www.dartdocs .org/documentation/http/latest/http/StreamedRequest-class.html
50
- [ Client.send ] : https://www.dartdocs .org/documentation/http/latest/http/Client/send.html
45
+ [ Request ] : https://pub.dartlang .org/documentation/http/latest/http/Request-class.html
46
+ [ StreamedRequest ] : https://pub.dartlang .org/documentation/http/latest/http/StreamedRequest-class.html
47
+ [ Client.send ] : https://pub.dartlang .org/documentation/http/latest/http/Client/send.html
51
48
52
49
This package is designed to be composable. This makes it easy for external
53
50
libraries to work with one another to add behavior to it. Libraries wishing to
54
51
add behavior should create a subclass of [ BaseClient] [ ] that wraps another
55
52
[ Client] [ ] and adds the desired behavior:
56
53
57
- [ BaseClient ] : https://www.dartdocs .org/documentation/http/latest/http/BaseClient-class.html
58
- [ Client ] : https://www.dartdocs .org/documentation/http/latest/http/Client-class.html
54
+ [ BaseClient ] : https://pub.dartlang .org/documentation/http/latest/http/BaseClient-class.html
55
+ [ Client ] : https://pub.dartlang .org/documentation/http/latest/http/Client-class.html
59
56
60
57
``` dart
61
58
class UserAgentClient extends http.BaseClient {
@@ -70,25 +67,3 @@ class UserAgentClient extends http.BaseClient {
70
67
}
71
68
}
72
69
```
73
-
74
- ## Using on the Browser
75
-
76
- The HTTP library can be used on the browser via the [ BrowserClient] [ ] class in
77
- ` package:http/browser_client.dart ` . This client translates requests into
78
- XMLHttpRequests. For example:
79
-
80
- [ BrowserClient ] : https://www.dartdocs.org/documentation/http/latest/http.browser_client/BrowserClient-class.html
81
-
82
- ``` dart
83
- import 'dart:async';
84
- import 'package:http/browser_client.dart';
85
-
86
- main() async {
87
- var client = new BrowserClient();
88
- var url = '/whatsit/create';
89
- var response =
90
- await client.post(url, body: {'name': 'doodle', 'color': 'blue'});
91
- print('Response status: ${response.statusCode}');
92
- print('Response body: ${response.body}');
93
- }
94
- ```
0 commit comments