pub struct Client { /* private fields */ }Expand description
An HTTP client, which holds the defaults that outgoing requests start from, the hooks that see each request, and the connections the requests share.
The module helpers, such as client::get, use the application’s client. You may
also construct a client and start requests with its get and other methods. A
client you construct has its own settings and hooks, but it uses the current application’s
fakes and file paths. Clones of a client share connections until their transport settings
change.
| Setting | Default |
|---|---|
timeout | 30 seconds |
connect_timeout | 10 seconds |
user_agent | Laravel-Rust/ and the framework’s version |
Laravel registers a client with these defaults. To change them, register your own client as an instance. Because requests read an instance without building it first, requests also work inside other services’ factories:
use laravel::Application;
use laravel::config::Env;
use laravel::http::client::Client;
use laravel::time::ToSpan;
let app = Application::configure()
.replace_env(Env::default())
.with_services(|app| {
app.instance(
Client::new()
.timeout(10.seconds())
.user_agent("Acme/1.0")
.before_sending(|request| request.header("x-tenant", "acme"))
.after_response(|response| tracing::debug!(status = %response.status())),
);
})
.create();Implementations§
Source§impl Client
impl Client
Sourcepub fn get(&self, url: impl Into<String>) -> PendingRequest
pub fn get(&self, url: impl Into<String>) -> PendingRequest
Start a GET request using this client’s settings, hooks, and shared connections.
The request still uses the current application’s fakes and file paths.
use laravel::http::client::Client;
let client = Client::new().user_agent("Acme/1.0");
let response = client.get("https://example.com/users").await?;Sourcepub fn head(&self, url: impl Into<String>) -> PendingRequest
pub fn head(&self, url: impl Into<String>) -> PendingRequest
Start a HEAD request using this client.
Sourcepub fn post(&self, url: impl Into<String>) -> PendingRequest
pub fn post(&self, url: impl Into<String>) -> PendingRequest
Start a POST request using this client.
Sourcepub fn put(&self, url: impl Into<String>) -> PendingRequest
pub fn put(&self, url: impl Into<String>) -> PendingRequest
Start a PUT request using this client.
Sourcepub fn patch(&self, url: impl Into<String>) -> PendingRequest
pub fn patch(&self, url: impl Into<String>) -> PendingRequest
Start a PATCH request using this client.
Sourcepub fn delete(&self, url: impl Into<String>) -> PendingRequest
pub fn delete(&self, url: impl Into<String>) -> PendingRequest
Start a DELETE request using this client.
Sourcepub fn request(&self, method: Method, url: impl Into<String>) -> PendingRequest
pub fn request(&self, method: Method, url: impl Into<String>) -> PendingRequest
Start a request with any method using this client.
The request holds a shared handle to the client, so it can outlive this value. Clones of a client share its connection pool, and changing the transport settings on one clone gives that clone a new pool.
Sourcepub fn base_url(&self, url: impl Into<String>) -> PendingRequest
pub fn base_url(&self, url: impl Into<String>) -> PendingRequest
Start a reusable request template using this client and base URL.
use laravel::http::client::Client;
let api = Client::new().base_url("https://api.example.com").token("secret");
let response = api.get("/users").await?;Sourcepub fn timeout(self, timeout: impl Into<ZonedDateTimeArithmetic>) -> Client
pub fn timeout(self, timeout: impl Into<ZonedDateTimeArithmetic>) -> Client
Wait this long for each response, including its body, such as 10.seconds().
A request’s own timeout replaces this one. Zero means no
limit.
Sourcepub fn connect_timeout(
self,
timeout: impl Into<ZonedDateTimeArithmetic>,
) -> Client
pub fn connect_timeout( self, timeout: impl Into<ZonedDateTimeArithmetic>, ) -> Client
Wait this long to connect to a server. Zero means no limit.
Sourcepub fn configure_transport(
self,
configure: impl Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static,
) -> Client
pub fn configure_transport( self, configure: impl Fn(ClientBuilder) -> ClientBuilder + Send + Sync + 'static, ) -> Client
Configure the underlying reqwest transport, such as its certificates, proxy, or redirect policy.
The callbacks run in order when the client builds the transport, after it sets the connect timeout. Several first requests sent at once may run the callbacks at the same time, so keep them free of side effects. Request timeouts, hooks, retries, and fakes still apply.
Set response deadlines with timeout rather than on the transport. A
timeout set on the transport also limits streamed responses, even when the client’s
timeout is zero.
use laravel::http::client::{Client, reqwest};
let client = Client::new()
.configure_transport(|transport| transport.redirect(reqwest::redirect::Policy::none()));Sourcepub fn user_agent(self, user_agent: &str) -> Client
pub fn user_agent(self, user_agent: &str) -> Client
Send this User-Agent header with requests that don’t set their own.
A value that isn’t a valid header value fails each request the client sends with
Error::Invalid.
Sourcepub fn before_sending(
self,
hook: impl Fn(PendingRequest) -> PendingRequest + Send + Sync + 'static,
) -> Client
pub fn before_sending( self, hook: impl Fn(PendingRequest) -> PendingRequest + Send + Sync + 'static, ) -> Client
Change every request before it’s sent, such as to add a header.
The hooks run once for each request, in the order you added them, before the client adds
its default headers and the body’s headers. To limit a hook to one API or to sign the
buffered body, inspect the request’s url,
method, header_value, or
body_bytes. Later hooks can still change the request, so
add a signing hook last.
use laravel::http::client::Client;
let client = Client::new().before_sending(|request| {
if request.url().is_ok_and(|url| url.host_str() == Some("api.example.com")) {
request.token("secret")
} else {
request
}
});Sourcepub fn after_response(
self,
hook: impl Fn(&Response) + Send + Sync + 'static,
) -> Client
pub fn after_response( self, hook: impl Fn(&Response) + Send + Sync + 'static, ) -> Client
Run a hook on every response, such as to record metrics.
The hook sees the response to each attempt, including retried and faked attempts. It sees a streamed response from the network as soon as the response begins, without its body. If the response’s status fails the request, the hook sees the start of the body that the request read to explain the failure.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Client
impl !RefUnwindSafe for Client
impl !UnwindSafe for Client
impl Forget for Client
impl Move for Client
impl Send for Clientwhere
Bytes: Send,
RawVecInner: Send,
Arc<dyn Error + Sync + Send>: Send,
Arc<dyn Fn(PendingRequest) -> PendingRequest + Sync + Send>: Send,
Arc<dyn Fn(&Response) + Sync + Send>: Send,
Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Sync + Send>: Send,
Arc<OnceLock<Result<Client, InvalidRequest>>>: Send,
impl Sync for Clientwhere
Bytes: Sync,
RawVecInner: Sync,
Arc<dyn Error + Sync + Send>: Sync,
Arc<dyn Fn(PendingRequest) -> PendingRequest + Sync + Send>: Sync,
Arc<dyn Fn(&Response) + Sync + Send>: Sync,
Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Sync + Send>: Sync,
Arc<OnceLock<Result<Client, InvalidRequest>>>: Sync,
impl Unpin for Clientwhere
*const u8: Unpin,
Align8<*mut ()>: Unpin,
&'static Vtable: Unpin,
*const u8 is !null: Unpin,
Arc<dyn Error + Sync + Send>: Unpin,
Arc<dyn Fn(PendingRequest) -> PendingRequest + Sync + Send>: Unpin,
Arc<dyn Fn(&Response) + Sync + Send>: Unpin,
Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Sync + Send>: Unpin,
Arc<OnceLock<Result<Client, InvalidRequest>>>: Unpin,
impl UnsafeUnpin for Clientwhere
*const u8: UnsafeUnpin,
Align8<*mut ()>: UnsafeUnpin,
&'static Vtable: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const ArcInner<dyn Error + Sync + Send> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Error + Sync + Send>>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(PendingRequest) -> PendingRequest + Sync + Send>>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(&Response) + Sync + Send>>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(ClientBuilder) -> ClientBuilder + Sync + Send>>: UnsafeUnpin,
*const ArcInner<OnceLock<Result<Client, InvalidRequest>>> is !null: UnsafeUnpin,
PhantomData<ArcInner<OnceLock<Result<Client, InvalidRequest>>>>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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 more