pub struct Client { /* private fields */ }
Expand description
An asynchronous Client
to make Requests with.
The Client has various configuration values to tweak, but the defaults
are set to what is usually the most commonly desired value. To configure a
Client
, use Client::builder()
.
The Client
holds a connection pool internally, so it is advised that
you create one and reuse it.
You do not have to wrap the Client
in an Rc
or Arc
to reuse it,
because it already uses an Arc
internally.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new() -> Client
pub fn new() -> Client
Constructs a new Client
.
§Panics
This method panics if a TLS backend cannot be initialized, or the resolver cannot load the system configuration.
Use Client::builder()
if you wish to handle the failure as an Error
instead of panicking.
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Create a ClientBuilder
specifically configured for WebSocket connections.
This method configures the ClientBuilder
to use HTTP/1.0 only, which is required for certain WebSocket connections.
Sourcepub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a GET
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn websocket<U: IntoUrl>(&self, url: U) -> WebSocketRequestBuilder
pub fn websocket<U: IntoUrl>(&self, url: U) -> WebSocketRequestBuilder
Upgrades the RequestBuilder
to perform a
websocket handshake. This returns a wrapped type, so you must do
this after you set up your request, and just before you send the
request.
Sourcepub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a POST
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a PUT
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a PATCH
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a DELETE
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder
pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder
Convenience method to make a HEAD
request to a URL.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder
Start building a Request
with the Method
and Url
.
Returns a RequestBuilder
, which will allow setting headers and
the request body before sending.
§Errors
This method fails whenever the supplied Url
cannot be parsed.
Sourcepub fn execute(
&self,
request: Request,
) -> impl Future<Output = Result<Response, Error>>
pub fn execute( &self, request: Request, ) -> impl Future<Output = Result<Response, Error>>
Executes a Request
.
A Request
can be built manually with Request::new()
or obtained
from a RequestBuilder with RequestBuilder::build()
.
You should prefer to use the RequestBuilder
and
RequestBuilder::send()
.
§Errors
This method fails if there was an error while sending request, redirect loop was detected or redirect limit was exhausted.
Source§impl Client
impl Client
Sourcepub fn user_agent(&self) -> Option<HeaderValue>
pub fn user_agent(&self) -> Option<HeaderValue>
Retrieves the User-Agent
header for this client.
This method returns the User-Agent
header value if it is set for this client.
§Returns
An Option<HeaderValue>
containing the User-Agent
header value if it is set, or None
if it is not.
Sourcepub fn headers(&self) -> HeaderMap
pub fn headers(&self) -> HeaderMap
Retrieves a headers for this client.
This method returns a HeaderMap
containing the headers for this client.
Note that this operation involves cloning the headers, which can be
expensive if the header map is large.
§Returns
A HeaderMap
containing the headers for this client.
Returns a String
of the header-value of all Cookie
in a Url
.
Injects a ‘Cookie’ into the ‘CookieStore’ for the specified URL.
This method accepts a collection of cookies, which can be either an owned
vector (Vec<HeaderValue>
) or a reference to a slice (&[HeaderValue]
).
It will convert the collection into an iterator and pass it to the
cookie_store
for processing.
§Parameters
url
: The URL associated with the cookies to be set.cookies
: A collection ofHeaderValue
items, either by reference or owned.
This method ensures that cookies are only set if at least one cookie exists in the collection.
Inserts a cookie into the CookieStore
for the specified URL.
§Note
This method requires the cookies
feature to be enabled.
Removes a cookie from the CookieStore
for the specified URL.
This method deletes a cookie with the given name from the client’s CookieStore
for the specified URL. It can be useful in scenarios where you want to remove
specific cookies to reset the client’s state or ensure that certain cookies are
not sent with subsequent requests.
§Parameters
url
: The URL associated with the cookie to be removed.name
: The name of the cookie to be removed.
§Note
This method requires the cookies
feature to be enabled.
Clears all cookies from the CookieStore
.
This method removes all cookies stored in the client’s CookieStore
.
It can be useful in scenarios where you want to reset the client’s state
or ensure that no cookies are sent with subsequent requests.
§Note
This method requires the cookies
feature to be enabled.
Sourcepub fn update(&self) -> ClientUpdate<'_>
pub fn update(&self) -> ClientUpdate<'_>
Returns a ClientUpdate
instance to modify the internal state of the Client
.
This method allows you to obtain a ClientUpdate
instance, which provides methods
to mutate the internal state of the Client
. This is useful when you need to modify
the client’s configuration or state after it has been created.
§Returns
A ClientUpdate<'_>
instance that can be used to modify the internal state of the Client
.
§Example
let client = rquest::Client::new();
client.update()
.headers(|headers| {
headers.insert("X-Custom-Header", HeaderValue::from_static("value"));
})
.apply()
.unwrap();
Sourcepub fn cloned(&self) -> Self
pub fn cloned(&self) -> Self
Clones the Client
into a new instance.
This method creates a new instance of the Client
by cloning its internal state.
The cloned client will have the same configuration and state as the original client,
but it will be a separate instance that can be used independently.
Note that this will still share the connection pool with the original Client
.
§Example
let client = rquest::Client::new();
let cloned_client = client.cloned();
// Use the cloned client independently
Trait Implementations§
Source§impl Service<Request> for &Client
impl Service<Request> for &Client
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service
, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready
method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready
method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready
method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>
)
to a different value, regardless of whether the future succeeds or
fails. Read more