rquest/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(test, deny(warnings))]
//! # rquest
//!
//! An ergonomic, all-in-one `JA3`/`JA4`/`HTTP2` fingerprint `HTTP`/`WebSocket` client.
//!
//! - Async Client
//! - Plain bodies, [JSON](#json), [urlencoded](#forms), [multipart], [websocket](#websocket)
//! - Headers Order
//! - Customizable [redirect policy](#redirect-policies)
//! - Cookies Store
//! - Uses [BoringSSL](#tls)
//! - HTTP [Proxies](#proxies)
//! - [Preconfigured](#preconfigured-tls) `TLS`/`HTTP2`/`Headers` settings
//! - Chrome / Safari / Edge / OkHttp [Fingerprint](#impersonate)
//! - [Changelog](https://github.com/0x676e67/rquest/blob/main/CHANGELOG.md)
//!
//! Additional learning resources include:
//!
//! - [The Rust Cookbook](https://rust-lang-nursery.github.io/rust-cookbook/web/clients.html)
//! - [Repository Examples](https://github.com/0x676e67/rquest/tree/master/examples)
//!
//! ## Impersonate
//!
//! The `impersonate` module provides a way to simulate various browser fingerprints.
//!
//! ```rust,no_run
//! use std::error::Error;
//! use rquest::tls::Impersonate;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! // Build a client to mimic Chrome129
//! let client = rquest::Client::builder()
//! .impersonate(Impersonate::Chrome129)
//! .build()?;
//!
//! // Use the API you're already familiar with
//! let resp = client.get("https://tls.peet.ws/api/all").send().await?;
//! println!("{}", resp.text().await?);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Websocket
//!
//! The `websocket` module provides a way to upgrade a connection to a websocket.
//!
//! ```rust,no_run
//! use futures_util::{SinkExt, StreamExt, TryStreamExt};
//! use rquest::{tls::Impersonate, Client, Message};
//! use std::error::Error;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! let websocket = Client::builder()
//! .impersonate(Impersonate::Chrome127)
//! .http1_only()
//! .build()?
//! .get("wss://echo.websocket.org")
//! .upgrade()
//! .send()
//! .await?
//! .into_websocket()
//! .await?;
//!
//! let (mut tx, mut rx) = websocket.split();
//!
//! tokio::spawn(async move {
//! for i in 1..11 {
//! tx.send(Message::Text(format!("Hello, World! #{i}")))
//! .await
//! .unwrap();
//! }
//! });
//!
//! while let Some(message) = rx.try_next().await? {
//! match message {
//! Message::Text(text) => println!("received: {text}"),
//! _ => {}
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Preconfigured-TLS
//! If you need to use a pre-configured TLS settings, you can use the [use_preconfigured_tls][preconfigured] method on the `ClientBuilder`.
//!
//! ```rust
//!use boring::ssl::{SslConnector, SslMethod};
//!use http::{header, HeaderValue};
//!use rquest::{
//! tls::{Http2Settings, ImpersonateSettings, TlsExtensionSettings},
//! HttpVersionPref,
//!};
//!use rquest::{PseudoOrder::*, SettingsOrder::*};
//!use std::error::Error;
//!
//!#[tokio::main]
//!async fn main() -> Result<(), Box<dyn Error>> {
//! // Create a pre-configured TLS settings
//! let settings = ImpersonateSettings::builder()
//! .tls((
//! SslConnector::builder(SslMethod::tls_client())?,
//! TlsExtensionSettings::builder()
//! .tls_sni(true)
//! .http_version_pref(HttpVersionPref::All)
//! .application_settings(true)
//! .pre_shared_key(true)
//! .enable_ech_grease(true)
//! .permute_extensions(true)
//! .build(),
//! ))
//! .http2(
//! Http2Settings::builder()
//! .initial_stream_window_size(6291456)
//! .initial_connection_window_size(15728640)
//! .max_concurrent_streams(1000)
//! .max_header_list_size(262144)
//! .header_table_size(65536)
//! .enable_push(false)
//! .headers_priority((0, 255, true))
//! .headers_pseudo_order([Method, Scheme, Authority, Path])
//! .settings_order(&[
//! HeaderTableSize,
//! EnablePush,
//! MaxConcurrentStreams,
//! InitialWindowSize,
//! MaxFrameSize,
//! MaxHeaderListSize,
//! EnableConnectProtocol,
//! ])
//! .build(),
//! )
//! .headers(Box::new(|headers| {
//! headers.insert(header::USER_AGENT, HeaderValue::from_static("rquest"));
//! }))
//! .build();
//!
//! // Build a client with pre-configured TLS settings
//! let client = rquest::Client::builder()
//! .use_preconfigured_tls(settings)
//! .build()?;
//!
//! // Use the API you're already familiar with
//! let resp = client.get("https://tls.peet.ws/api/all").send().await?;
//! println!("{}", resp.text().await?);
//!
//! Ok(())
//!}
//!
//! ```
//!
//! ## Making a GET request
//!
//! For a single request, you can use the [`get`][get] shortcut method.
//!
//! ```rust
//! # async fn run() -> Result<(), rquest::Error> {
//! let body = rquest::get("https://www.rust-lang.org")
//! .await?
//! .text()
//! .await?;
//!
//! println!("body = {:?}", body);
//! # Ok(())
//! # }
//! ```
//!
//! **NOTE**: If you plan to perform multiple requests, it is best to create a
//! [`Client`][client] and reuse it, taking advantage of keep-alive connection
//! pooling.
//!
//! ## Making POST requests (or setting request bodies)
//!
//! There are several ways you can set the body of a request. The basic one is
//! by using the `body()` method of a [`RequestBuilder`][builder]. This lets you set the
//! exact raw bytes of what the body should be. It accepts various types,
//! including `String` and `Vec<u8>`. If you wish to pass a custom
//! type, you can use the `rquest::Body` constructors.
//!
//! ```rust
//! # use rquest::Error;
//! #
//! # async fn run() -> Result<(), Error> {
//! let client = rquest::Client::new();
//! let res = client.post("http://httpbin.org/post")
//! .body("the exact body that is sent")
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Forms
//!
//! It's very common to want to send form data in a request body. This can be
//! done with any type that can be serialized into form data.
//!
//! This can be an array of tuples, or a `HashMap`, or a custom type that
//! implements [`Serialize`][serde].
//!
//! ```rust
//! # use rquest::Error;
//! #
//! # async fn run() -> Result<(), Error> {
//! // This will POST a body of `foo=bar&baz=quux`
//! let params = [("foo", "bar"), ("baz", "quux")];
//! let client = rquest::Client::new();
//! let res = client.post("http://httpbin.org/post")
//! .form(¶ms)
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### JSON
//!
//! There is also a `json` method helper on the [`RequestBuilder`][builder] that works in
//! a similar fashion the `form` method. It can take any value that can be
//! serialized into JSON. The feature `json` is required.
//!
//! ```rust
//! # use rquest::Error;
//! # use std::collections::HashMap;
//! #
//! # #[cfg(feature = "json")]
//! # async fn run() -> Result<(), Error> {
//! // This will POST a body of `{"lang":"rust","body":"json"}`
//! let mut map = HashMap::new();
//! map.insert("lang", "rust");
//! map.insert("body", "json");
//!
//! let client = rquest::Client::new();
//! let res = client.post("http://httpbin.org/post")
//! .json(&map)
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Redirect Policies
//!
//! By default, a `Client` will automatically handle HTTP redirects, having a
//! maximum redirect chain of 10 hops. To customize this behavior, a
//! [`redirect::Policy`][redirect] can be used with a `ClientBuilder`.
//!
//! ## Cookies
//!
//! The automatic storing and sending of session cookies can be enabled with
//! the [`cookie_store`][ClientBuilder::cookie_store] method on `ClientBuilder`.
//!
//! ## Proxies
//!
//! **NOTE**: System proxies are enabled by default.
//!
//! System proxies look in environment variables to set HTTP or HTTPS proxies.
//!
//! `HTTP_PROXY` or `http_proxy` provide HTTP proxies for HTTP connections while
//! `HTTPS_PROXY` or `https_proxy` provide HTTPS proxies for HTTPS connections.
//! `ALL_PROXY` or `all_proxy` provide proxies for both HTTP and HTTPS connections.
//! If both the all proxy and HTTP or HTTPS proxy variables are set the more specific
//! HTTP or HTTPS proxies take precedence.
//!
//! These can be overwritten by adding a [`Proxy`] to `ClientBuilder`
//! i.e. `let proxy = rquest::Proxy::http("https://secure.example")?;`
//! or disabled by calling `ClientBuilder::no_proxy()`.
//!
//! `socks` feature is required if you have configured socks proxy like this:
//!
//! ```bash
//! export https_proxy=socks5://127.0.0.1:1086
//! ```
//! ## TLS
//!
//! By default, clients will utilize BoringSSL transport layer security to connect to HTTPS targets.
//!
//! - Various parts of TLS can also be configured or even disabled on the
//! `ClientBuilder`.
//!
//! ## Optional Features
//!
//! The following are a list of [Cargo features][cargo-features] that can be
//! enabled or disabled:
//!
//! - **boring-tls** *(enabled by default)*: Provides TLS support to connect
//! over HTTPS.
//! - **websocket**: Provides websocket support.
//! - **cookies**: Provides cookie session support.
//! - **gzip**: Provides response body gzip decompression.
//! - **brotli**: Provides response body brotli decompression.
//! - **zstd**: Provides response body zstd decompression.
//! - **deflate**: Provides response body deflate decompression.
//! - **json**: Provides serialization and deserialization for JSON bodies.
//! - **multipart**: Provides functionality for multipart forms.
//! - **stream**: Adds support for `futures::Stream`.
//! - **socks**: Provides SOCKS5 proxy support.
//! - **hickory-dns**: Enables a hickory-dns async resolver instead of default
//! threadpool using `getaddrinfo`.
//!
//! [hyper]: http://hyper.rs
//! [client]: ./struct.Client.html
//! [response]: ./struct.Response.html
//! [get]: ./fn.get.html
//! [builder]: ./struct.RequestBuilder.html
//! [serde]: http://serde.rs
//! [redirect]: crate::redirect
//! [Proxy]: ./struct.Proxy.html
//! [preconfigured]: ./struct.ClientBuilder.html#method.use_preconfigured_tls
//! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section
/// Re-export of boring to keep versions in check
#[cfg(feature = "boring-tls")]
pub use boring;
#[cfg(feature = "boring-tls")]
pub use boring_sys;
#[cfg(feature = "hickory-dns")]
pub use hickory_resolver::config::LookupIpStrategy;
pub use http::header;
pub use http::Method;
pub use http::{StatusCode, Version};
pub use url::Url;
// universal mods
#[macro_use]
mod error;
mod into_url;
mod response;
pub use self::error::{Error, Result};
pub use self::into_url::IntoUrl;
pub use self::response::ResponseBuilderExt;
/// Shortcut method to quickly make a `GET` request.
///
/// See also the methods on the [`rquest::Response`](./struct.Response.html)
/// type.
///
/// **NOTE**: This function creates a new internal `Client` on each call,
/// and so should not be used if making many requests. Create a
/// [`Client`](./struct.Client.html) instead.
///
/// # Examples
///
/// ```rust
/// # async fn run() -> Result<(), rquest::Error> {
/// let body = rquest::get("https://www.rust-lang.org").await?
/// .text().await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This function fails if:
///
/// - native TLS backend cannot be initialized
/// - supplied `Url` cannot be parsed
/// - there was an error while sending request
/// - redirect limit was exhausted
pub async fn get<T: IntoUrl>(url: T) -> crate::Result<Response> {
Client::builder().build()?.get(url).send().await
}
/// Opens a websocket at the specified URL.
///
/// This is a shorthand for creating a request, sending it, and turning the
/// response into a websocket.
#[cfg(feature = "websocket")]
pub async fn websocket<T: IntoUrl>(url: T) -> crate::Result<WebSocket> {
Client::builder()
.http1_only()
.build()?
.get(url)
.upgrade()
.send()
.await?
.into_websocket()
.await
}
fn _assert_impls() {
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
fn assert_clone<T: Clone>() {}
assert_send::<Client>();
assert_sync::<Client>();
assert_clone::<Client>();
assert_send::<Request>();
assert_send::<RequestBuilder>();
#[cfg(not(target_arch = "wasm32"))]
{
assert_send::<Response>();
}
assert_send::<Error>();
assert_sync::<Error>();
}
#[cfg(test)]
doc_comment::doctest!("../README.md");
#[cfg(feature = "multipart")]
pub use self::client::multipart;
#[cfg(feature = "websocket")]
pub use self::client::websocket::{
CloseCode, Message, WebSocket, WebSocketRequestBuilder, WebSocketResponse,
};
pub use self::client::{
Body, Client, ClientBuilder, HttpVersionPref, Request, RequestBuilder, Response, Upgraded,
};
pub use self::proxy::{NoProxy, Proxy};
#[cfg(feature = "boring-tls")]
pub use hyper::{PseudoOrder, SettingsOrder};
mod client;
mod connect;
#[cfg(feature = "cookies")]
pub mod cookie;
pub mod dns;
mod proxy;
pub mod redirect;
#[cfg(feature = "boring-tls")]
pub mod tls;
mod util;