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

Create new request based on the provided url.

To get a Response you need to pass Request to the fetch function.

MDN reference

Set headers for this request. It will replace any existing headers.

Set specific header.

Set HTTP method. Default method is GET.

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.

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.

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.

Set request body to a provided string. It will also set Content-Type header to text/plain; charset=utf-8.

Set request body to the provided bytes. It will also set Content-Type header to application/octet-stream.

Set request body to the provided form data object. It will also set Content-Type header to multipart/form-data.

Set request mode.

MDN reference

Set request credentials.

MDN reference

Set request cache mode.

MDN reference

Set request redirect mode.

MDN reference

Set request referrer.

MDN reference

Set request referrer policy.

MDN reference

Set request subresource integrity.

MDN reference

Set request timeout in milliseconds.

Get the request controller that allows to abort request or disable request’s timeout.

Example
let (request, controller) = Request::new("http://example.com").controller();

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Converts to this type from the input type.

Converts to this type from the input type.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.