reqwest/blocking/
mod.rs

1//! A blocking Client API.
2//!
3//! The blocking `Client` will block the current thread to execute, instead
4//! of returning futures that need to be executed on a runtime.
5//!
6//! Conversely, the functionality in `reqwest::blocking` must *not* be executed
7//! within an async runtime, or it will panic when attempting to block. If
8//! calling directly from an async function, consider using an async
9//! [`reqwest::Client`][crate::Client] instead. If the immediate context is only
10//! synchronous, but a transitive caller is async, consider changing that caller
11//! to use [`tokio::task::spawn_blocking`] around the calls that need to block.
12//!
13//! # Optional
14//!
15//! This requires the optional `blocking` feature to be enabled.
16//!
17//! # Making a GET request
18//!
19//! For a single request, you can use the [`get`] shortcut method.
20//!
21//! ```rust
22//! # use reqwest::{Error, Response};
23//!
24//! # fn run() -> Result<(), Error> {
25//! let body = reqwest::blocking::get("https://www.rust-lang.org")?
26//!     .text()?;
27//!
28//! println!("body = {body:?}");
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! Additionally, the blocking [`Response`] struct implements Rust's
34//! `Read` trait, so many useful standard library and third party crates will
35//! have convenience methods that take a `Response` anywhere `T: Read` is
36//! acceptable.
37//!
38//! **NOTE**: If you plan to perform multiple requests, it is best to create a
39//! [`Client`] and reuse it, taking advantage of keep-alive connection pooling.
40//!
41//! # Making POST requests (or setting request bodies)
42//!
43//! There are several ways you can set the body of a request. The basic one is
44//! by using the `body()` method of a [`RequestBuilder`]. This lets you set the
45//! exact raw bytes of what the body should be. It accepts various types,
46//! including `String`, `Vec<u8>`, and `File`. If you wish to pass a custom
47//! Reader, you can use the `reqwest::blocking::Body::new()` constructor.
48//!
49//! ```rust
50//! # use reqwest::Error;
51//! #
52//! # fn run() -> Result<(), Error> {
53//! let client = reqwest::blocking::Client::new();
54//! let res = client.post("http://httpbin.org/post")
55//!     .body("the exact body that is sent")
56//!     .send()?;
57//! # Ok(())
58//! # }
59//! ```
60//!
61//! ## And More
62//!
63//! Most features available to the asynchronous `Client` are also available,
64//! on the blocking `Client`, see those docs for more.
65
66mod body;
67mod client;
68#[cfg(feature = "multipart")]
69pub mod multipart;
70mod request;
71mod response;
72mod wait;
73
74pub use self::body::Body;
75pub use self::client::{Client, ClientBuilder};
76pub use self::request::{Request, RequestBuilder};
77pub use self::response::Response;
78
79/// Shortcut method to quickly make a *blocking* `GET` request.
80///
81/// **NOTE**: This function creates a new internal `Client` on each call,
82/// and so should not be used if making many requests. Create a
83/// [`Client`](./struct.Client.html) instead.
84///
85/// # Examples
86///
87/// ```rust
88/// # fn run() -> Result<(), reqwest::Error> {
89/// let body = reqwest::blocking::get("https://www.rust-lang.org")?
90///     .text()?;
91/// # Ok(())
92/// # }
93/// # fn main() { }
94/// ```
95///
96/// # Errors
97///
98/// This function fails if:
99///
100/// - the native TLS backend cannot be initialized,
101/// - the supplied `Url` cannot be parsed,
102/// - there was an error while sending request,
103/// - a redirect loop was detected,
104/// - the redirect limit was exhausted, or
105/// - the total download time exceeds 30 seconds.
106pub fn get<T: crate::IntoUrl>(url: T) -> crate::Result<Response> {
107    Client::builder().build()?.get(url).send()
108}