Expand description
§rquest
An ergonomic, all-in-one JA3/JA4/HTTP2 fingerprint HTTP/WebSocket client.
- Async Client
- Plain bodies, JSON, urlencoded, multipart, websocket
- Headers Order
- Customizable redirect policy
- Cookies Store
- Uses BoringSSL
- HTTP Proxies
- Preconfigured
TLS/HTTP2/Headerssettings - Chrome / Safari / Edge / OkHttp Fingerprint
- Changelog
Additional learning resources include:
§Impersonate
The impersonate module provides a way to simulate various browser fingerprints.
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.
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 method on the ClientBuilder.
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 shortcut method.
let body = rquest::get("https://www.rust-lang.org")
.await?
.text()
.await?;
println!("body = {:?}", body);NOTE: If you plan to perform multiple requests, it is best to create a
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. 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.
let client = rquest::Client::new();
let res = client.post("http://httpbin.org/post")
.body("the exact body that is sent")
.send()
.await?;§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.
// 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?;§JSON
There is also a json method helper on the RequestBuilder 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.
// 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?;§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 can be used with a ClientBuilder.
§Cookies
The automatic storing and sending of session cookies can be enabled with
the 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:
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 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.
Re-exports§
pub use boring;pub use boring_sys;
Modules§
- HTTP Cookies
- DNS resolution
- HTTP header types
- multipart/form-data
- Redirect Handling
- TLS configuration
Structs§
- An asynchronous request body.
- An asynchronous
Clientto make Requests with. - A
ClientBuildercan be used to create aClientwith custom configuration. - The Errors that may occur when processing a
Request. - The Request Method (VERB)
- A configuration for filtering out requests that shouldn’t be proxied
- Configuration of a proxy that a
Clientshould pass requests to. - A request which can be executed with
Client::execute(). - A builder to construct the properties of a
Request. - A Response to a submitted
Request. - An HTTP status code (
status-codein RFC 7230 et al.). - An upgraded HTTP connection.
- A parsed URL record.
- Represents a version of the HTTP spec.
- A websocket connection
- Wrapper for
RequestBuilderthat performs the websocket handshake when sent. - The server’s response to the websocket upgrade request.
Enums§
- Status code used to indicate why an endpoint is closing the
WebSocketconnection.1 - A
HttpVersionPrefis used to set the HTTP version preference. - The lookup ip strategy
- A
WebSocketmessage, which can be a text string or binary data.
Traits§
- A trait to try to convert some type into a
Url. - Extension trait for http::response::Builder objects
Functions§
- Shortcut method to quickly make a
GETrequest. - Opens a websocket at the specified URL.
Type Aliases§
- A
Resultalias where theErrcase isrquest::Error.