pub struct Request<'a> { /* private fields */ }
Expand description
Its methods configure the request, and handle the response. Many of them return the original struct, and are intended to be used chained together.
Implementations
sourceimpl<'a> Request<'a>
impl<'a> Request<'a>
sourcepub fn new(url: impl Into<Cow<'a, str>>) -> Self
pub fn new(url: impl Into<Cow<'a, str>>) -> Self
sourcepub fn headers(self, headers: Headers<'a>) -> Self
pub fn headers(self, headers: Headers<'a>) -> Self
Set headers for this request. It will replace any existing headers.
sourcepub fn body(self, body: JsValue) -> Self
pub fn body(self, body: JsValue) -> Self
Set request body to provided JsValue
. Consider using json
, text
or bytes
methods instead.
Panics
This method will panic when request method is GET or HEAD.
sourcepub fn body_ref(self, body: &'a JsValue) -> Self
pub fn body_ref(self, body: &'a JsValue) -> Self
Set request body to provided JsValue
by reference. Consider using
json
, text
or bytes
methods instead.
Panics
This method will panic when request method is GET or HEAD.
sourcepub fn json<T: Serialize + ?Sized>(self, data: &T) -> Result<Self>
pub fn json<T: Serialize + ?Sized>(self, data: &T) -> Result<Self>
Set request body by JSON encoding provided data.
It will also set Content-Type
header to application/json; charset=utf-8
.
Errors
This method can fail if JSON serialization fail. It will then
return FetchError::SerdeError
.
sourcepub fn text(self, text: impl AsRef<str>) -> Self
pub fn text(self, text: impl AsRef<str>) -> Self
Set request body to a provided string.
It will also set Content-Type
header to text/plain; charset=utf-8
.
sourcepub fn bytes(self, bytes: impl AsRef<[u8]>) -> Self
pub fn bytes(self, bytes: impl AsRef<[u8]>) -> Self
Set request body to the provided bytes.
It will also set Content-Type
header to application/octet-stream
.
sourcepub fn form_data(self, form_data: FormData) -> Self
pub fn form_data(self, form_data: FormData) -> Self
Set request body to the provided form data object.
It will also set Content-Type
header to multipart/form-data
.
sourcepub const fn mode(self, mode: RequestMode) -> Self
pub const fn mode(self, mode: RequestMode) -> Self
Set request mode.
sourcepub const fn credentials(self, credentials: RequestCredentials) -> Self
pub const fn credentials(self, credentials: RequestCredentials) -> Self
Set request credentials.
sourcepub const fn cache(self, cache: RequestCache) -> Self
pub const fn cache(self, cache: RequestCache) -> Self
Set request cache mode.
sourcepub const fn redirect(self, redirect: RequestRedirect) -> Self
pub const fn redirect(self, redirect: RequestRedirect) -> Self
Set request redirect mode.
sourcepub const fn referrer_policy(self, referrer_policy: ReferrerPolicy) -> Self
pub const fn referrer_policy(self, referrer_policy: ReferrerPolicy) -> Self
Set request referrer policy.
sourcepub fn controller(self) -> (Self, RequestController)
pub fn controller(self) -> (Self, RequestController)
Get the request controller that allows to abort request or disable request’s timeout.
Example
let (request, controller) = Request::new("http://example.com").controller();
sourcepub async fn fetch(self) -> Result<Response>
pub async fn fetch(self) -> Result<Response>
Fetch request. It’s a chainable alternative to fetch(request)
.
Example
orders.perform_cmd({
let message = model.new_message.clone();
async { Msg::Fetched(send_message(message).await) }
});
...
async fn send_message(new_message: String) -> fetch::Result<shared::SendMessageResponseBody> {
Request::new(get_request_url())
.method(Method::Post)
.json(&shared::SendMessageRequestBody { text: new_message })?
.fetch()
.await?
.check_status()?
.json()
.await
}
Errors
fetch
will return Err
only on network errors. This means that
even if you get Ok
from this function, you still need to check
Response
status for HTTP errors.
Trait Implementations
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Request<'a>
impl<'a> !Send for Request<'a>
impl<'a> !Sync for Request<'a>
impl<'a> Unpin for Request<'a>
impl<'a> !UnwindSafe for Request<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more