1 | //! HTTP Client |
2 | //! |
3 | //! There are two levels of APIs provided for construct HTTP clients: |
4 | //! |
5 | //! - The higher-level [`Client`](Client) type. |
6 | //! - The lower-level [`conn`](conn) module. |
7 | //! |
8 | //! # Client |
9 | //! |
10 | //! The [`Client`](Client) is the main way to send HTTP requests to a server. |
11 | //! The default `Client` provides these things on top of the lower-level API: |
12 | //! |
13 | //! - A default **connector**, able to resolve hostnames and connect to |
14 | //! destinations over plain-text TCP. |
15 | //! - A **pool** of existing connections, allowing better performance when |
16 | //! making multiple requests to the same hostname. |
17 | //! - Automatic setting of the `Host` header, based on the request `Uri`. |
18 | //! - Automatic request **retries** when a pooled connection is closed by the |
19 | //! server before any bytes have been written. |
20 | //! |
21 | //! Many of these features can configured, by making use of |
22 | //! [`Client::builder`](Client::builder). |
23 | //! |
24 | //! ## Example |
25 | //! |
26 | //! For a small example program simply fetching a URL, take a look at the |
27 | //! [full client example](https://github.com/hyperium/hyper/blob/master/examples/client.rs). |
28 | //! |
29 | //! ``` |
30 | //! # #[cfg (all(feature = "tcp" , feature = "client" , any(feature = "http1" , feature = "http2" )))] |
31 | //! # async fn fetch_httpbin() -> hyper::Result<()> { |
32 | //! use hyper::{body::HttpBody as _, Client, Uri}; |
33 | //! |
34 | //! let client = Client::new(); |
35 | //! |
36 | //! // Make a GET /ip to 'http://httpbin.org' |
37 | //! let res = client.get(Uri::from_static("http://httpbin.org/ip" )).await?; |
38 | //! |
39 | //! // And then, if the request gets a response... |
40 | //! println!("status: {}" , res.status()); |
41 | //! |
42 | //! // Concatenate the body stream into a single buffer... |
43 | //! let buf = hyper::body::to_bytes(res).await?; |
44 | //! |
45 | //! println!("body: {:?}" , buf); |
46 | //! # Ok(()) |
47 | //! # } |
48 | //! # fn main () {} |
49 | //! ``` |
50 | |
51 | #[cfg (feature = "tcp" )] |
52 | pub use self::connect::HttpConnector; |
53 | |
54 | pub mod connect; |
55 | #[cfg (all(test, feature = "runtime" ))] |
56 | mod tests; |
57 | |
58 | cfg_feature! { |
59 | #![any(feature = "http1" , feature = "http2" )] |
60 | |
61 | pub use self::client::{Builder, Client, ResponseFuture}; |
62 | |
63 | mod client; |
64 | pub mod conn; |
65 | pub(super) mod dispatch; |
66 | mod pool; |
67 | pub mod service; |
68 | } |
69 | |